beckah beckah - 3 months ago 13
Python Question

uint32 vs uint64: What bases do I need for the 'int()' function to work properly

If I have two hex-strings and want to convert one to an 32-bit unsigned integer and the other to a 64-bit unsigned integer, what bases would I provide the

int()
function?

Answer

So I goofed up and int() does not determine the size or sign of your hex string.

By definition, hex is 16. So you would put in your string with the hex base of 16

int('A1S31231', 16)

The issue between 32 bit and 64 bit was simply the size of the string put in as an argument.

By virtue of their size,

2 hex characters = 1 byte

So if I had a 64 bit int, it would be 8 bytes or a 16 character hex string. If I had a 32 bit int, it would be 4 bytes or 8 character hex string.

Based off Duncan's answer: In order to make your result unsigned. You would need to take your result and & them with their proper mask.

If you're looking to go from hex to an uint32 you would do the aforementioned int() conversion and then

result & 0xffffffff

If you wanted to go from hex to uint64 you would do the aforementioned int() conversion and then

result & 0xffffffffffffffff
Comments