Hexadecimal bytes as variable converted to binary bits

You cannot use the notation 16^^ with variable arguments, since it does not get "evaluated" like normal functions, but rather it is just a way of writing integers in different bases. For more information you can look at this question.

A way to implement what you want would be

hexToBinary[hexstring_String] := IntegerDigits[FromDigits[hexstring,16],2];

To turn the list of binaries back into hex, you could use

binaryToHex[binary_List] := IntegerString[FromDigits[binary,2],16];

So

hexToBinary["c12b"]
{1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1}
 binaryToHex[{1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1}]
"c12b"

EDIT

As a workaround to allow simple user input you could use

hexToBinary[hex_] /; StringMatchQ[ToString[hex],RegularExpression["[A-Fa-f0-9]*"]] := 
    IntegerDigits[FromDigits[ToString@hex,16],2];

hexToBinary[c12b]
{1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1}

Of course, this could cause problems if for example someone feeds in an already defined variable, but the StringMatchQ should prevent at least some of them.


FromDigits is fast but it does not catch invalid inputs.

IntegerDigits[FromDigits["C12BZ", 16], 2]

{1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1}

Interpreter is slower but it will balk at invalid inputs.

IntegerDigits[Interpreter["HexInteger"]["C12BZ"], 2]

enter image description here