# Hex Representation in Mathematica and Python

I want to convert a code from Mathematica to Python.

I am using the following code to convert a code to hexadecimal, binary and so on:

    BinaryWrite[*file*, 8192, "Integer16"]


But I'd like to see the hex representation, so I am using this code:

BaseForm[8192, 16]


So I get the following answer:

$2000_{16}$

I am trying to convert this program to Python. If I use hex(8192) I get 0x2000.

Is Mathematica writing the hex file as 2000 or 0x2000? Can I just use hex(number) in Python?

-
"Integer16" has nothing to do with hexadecimal, it tells Mathematica to output the number as a signed 16 bit integer. Reading integers from binary file in Python – ssch Oct 29 '13 at 19:21
Ah, I think that's my problem, but I still don't understand completely. I even searched more about this. So how would it be printed a binary as a "Integer16" or as a "Integer32"? Which function can I use to see the result in those different binaries? – Zhozer Oct 30 '13 at 1:10
for instance: ImportString[ExportString[31, "Integer32"], "Bit"] However if you just want to get your data from mathematica I think the easiest is to Export["data.csv", data] and use the Python csv module – ssch Oct 30 '13 at 10:09
explains the stray " 0x" in the python form: ..stackoverflow.com/questions/10218164/… – george2079 Oct 30 '13 at 16:48
Thanks, @ssch, but I want to convert a binary compiler to Python, since the other person that needs that do not have Mathematica. I thin bin(np.int16(31)) should be the same as ImportString[ExportString[31, "Integer32"], "Bit"]. What do you think? – Zhozer Oct 30 '13 at 18:07

IntegerString[] is the equivalent of hex() in python..

IntegerString[8192,16] -> "2000"


Not sure what you mean by "hex file", one doesnt normally write the ascii hex digits to a file, though you might want to look at a hex dump of your binary file and compare with the above for debugging purpose.

Worth a note, IntegerDigits gives a "BigEndian" representation, regardless of your native machine byte ordering (as does python's hex()), you may need to reverse order if you want to compare with a little endian file:

// addressing your comment "Integer32" gives you a 32 bit, or 8 byte binary representation which can handle numbers larger than 2^16-1 (well 2^15-1 for signed integers). To "see" such in "hex" you use the same function, ie

IntegerString[2999999999,16]  -> "b2d05dff"


You can verify python's hex() gives the same result. Note the 16 here is the base (hex) not the binary bit length. Indeed mathematica can handle arbitrarily large numbers so,

IntegerString[99999999999999999999999999999999999999999999999999999, 16]
-> "10b46c6cdd6e3e0828f4db456ff0c8e9fffffffffffff"


which would be ~360 bits in a binary (base 2) representation.

These things are worth understanding if you are dealing with large data sets, as binary file io is typically much faster than ascii based.

-
Thanks, @george2079, but I noticed that I do not need a hex but a bin with signed 16-bit and signed 32-bit. I think it's a bit more difficult than convert to hex or base 32. – Zhozer Oct 30 '13 at 18:12
no its not difficult at all, BinaryWrite[] supports both formats no worries --- what problem are you having exactly ? – george2079 Oct 31 '13 at 12:39
Now I understood what is this Integer16. What I need to do is to find an equivalent way in Python to BinaryWrite[...,"Integer16(32)"]`. Do you have an idea? – Zhozer Nov 1 '13 at 21:16