I am working with hexadecimal bytes but need to shift them into lists of binary bits for a few operations and then back again.
For example, I would like C12B to turn into {1,1,0,0,0,0,0,1,0,0,1,0,1,0,1,1} for evaluations, so BaseForm[c12b,16]
is out of the question for me, as far as I can tell.
Trying IntegerDigits[16^^C12B,2]
gets me what I need, but my problem is if I try to store the hex value as a variable for use in a function.
Let's say I have a code:
hexToBinary[hexstring_]:=
Return[IntegerDigits[16^^hexstring,2]];
I realize I am no expert programmer, so there are probably small issues with this I cannot foresee, but 16^^hexstring
won't evaluate, as it thinks "hexstring" is the number I am trying to evaluate and tells me the letters are too large for that number base. Additionally, the set delayed fails to work in this case, and the cell evaluates immediately, returning the error just described. In fact, hexstring never turns green inside the IntegerDigits part.
I ran Print[Head[c12b]]
earlier, and Mathematica told me it's treating this as a "symbol."
I found out that if I input the hex in the form 0xc12b, Mathematica now thinks it's an integer, but this doesn't solve my problem of saving the value to a variable and then taking 16^^variable
.
Is there any feasible way around this? I am also going to have to turn the binary list back to Hex at some point, and I imagine I will run into similar problems on that end, but I have not gotten that far yet.
EDIT: If I make a function like Hausdorff suggested:
hexToBinary[hexstring_String] := IntegerDigits[FromDigits[hexstring,16],2];
Then that fixes my problem if and only if the hex value is put into this function as a string that has been saved under a variable, like:
input="c12b"
, followed by hexToBinary[input]
. But part of my question still exists: is there a way to save the hex value as an integer variable, like input=c12b
? Mathematica thinks c12b is another variable I have not yet defined if I try this.
The reason I ask this is not because I'm too lazy to put quotes around the strings but because I wanted to know if there is any way to do exactly what has been done here but without inputting it as a string in quotation marks. If I have to have the quotation marks, then so be it! The solution hints so far have been life-saving.
IntegerDigits[FromDigits["C12B", 16], 2]
$\endgroup$c12b
as a variable (you saw yourself thatPrint[Head[c12b]]
wasSymbol
), which is why the special format using^^
exists. However, Mathematica will store any hex number you input this way as a normal integer internally. $\endgroup$""
to get string format. $\endgroup$text1=IntegerDigits[FromDigits["YOURHEX1HERE",16],2]; text2=IntegerDigits[FromDigits["YOURHEX2HERE",16],2]; Function[text1,text2];
all in the same cell so they are evaluated simultaneously and the hex values are forced in in the proper format. I was trying to see if there was a way to just have my users do aFunction[YOURHEX1HERE,YOURHEX2HERE]
without causing a cascade of problems. $\endgroup$ToString
first. I just edited my answer with an example. $\endgroup$