Search code examples
structfloating-pointpack

using struct.pack to pack a numpy.float32 into 4 bytes


Hey all, I'm having a little trouble packing a numpy.float32 using the struct.pack function.

f32 = 38.2
struct.pack('f', f32)

The hexadecimal representation of 38.2, in 32 bits, is 0x4218CCCD. however, when I use the python terminal to run the preceding code (after importing the appropriate modules), the output is:

'\xcd\xcc\x18B'

I don't understand why it's leaving out the \x42 that should be before the B.

I am running 32 bit version of python 2.7 on a 64 bit machine. Any ideas?

Thanks in advance.


Solution

  • You got what you wanted.

    >>> "\x42" == "B"
    True