# Math Help - Mathematica - how to read in a number in hex and convert it to binary?

1. ## Mathematica - how to read in a number in hex and convert it to binary?

Hi, newbie question I'm afraid, but I couldn't work out how to do this from the help. I'm using Mathematica 5.2.

I've got a textfile containing a long list of numbers in hex. I can read them in one by one, using e.g.

Then I would like to turn each number into a fixed-length binary number, using something like

IntegerDigits[16^^p, 2, 27]

The problem is, this doesn't work. The argument of ^^ seems to need to be an actual number in hex, e.g. 16^A12B3, rather than a variable.

Will be grateful for any help with this.
Thanks!
Michael

2. Here's one idea: I save three hex numbers into a file as strings, then read them back as strings, then do the binary conversion:

Export["myHexFile.txt", {"f", "abc03b", "100fab"}]
IntegerDigits[FromDigits[#], 2, 27] & /@ myHexList

You may need to work on this a bit to get it to work for your application depending how you have the numbers formatted in the file. I'm not sure about the 16^^num format. Also, the last command is a "mapping" which takes each element in MyHexList, and passes it to the IntegerDigit routine at the placeholder #. See "Map" command.

3. Originally Posted by shawsend
Here's one idea: I save three hex numbers into a file as strings, then read them back as strings, then do the binary conversion:

Export["myHexFile.txt", {"f", "abc03b", "100fab"}]
IntegerDigits[FromDigits[#], 2, 27] & /@ myHexList

You may need to work on this a bit to get it to work for your application depending how you have the numbers formatted in the file. I'm not sure about the 16^^num format. Also, the last command is a "mapping" which takes each element in MyHexList, and passes it to the IntegerDigit routine at the placeholder #. See "Map" command.
Hi, thanks for this. Still doesn't work though. My data is a list of about 30 thousand 256-character hex numbers, separated by newlines, which I need to read in one by one. I can do this using

or

I seem to be getting into a complete mess. First, 16^^ doesn't seem to be able to take a variable as its argument. (Is there a way to use standard Mathematics function notation for ^^, using square brackets? It's surely a function in all but name, e.g.16^^A = 10 essentially means function(16, "A")= the decimal number 10, unless I'm wrong).

Second, how do I get the number I've read in to be accepted as an integer? I've tried replacing "Expression" with "Number" and "Integer", but that doesn't work. I can assign the input to a variable, e.g.

p=%

although then if I do something like

p+1

I just get

1 + A03B2174381BAC01025.......

Thanks again.

Michael

4. Read returns a string with "string" specified so 1+mynum is 1 plus a string. Use FromDigits to convert it to a number:

So I put three hex numbers separated by new lines into a text file "secondtextfile.txt" using just Notepad this time:

ffaa10345
aa1234b
cc10ff

Then used the following code to read the first one in:

Code:
mynum = Read["secondtextfile.txt", String]
1 + mynum
myrealnum = FromDigits[mynum, 16]
1 + myrealnum
Which gave me:

"ffaa10345"

1 + "ffaa10345"

68629365573

68629365574