Trey
Trey

Reputation: 348

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.

Upvotes: 1

Views: 2151

Answers (2)

Thomas Minor
Thomas Minor

Reputation: 657

\x42 corresponds to ASCII B.

Upvotes: 0

MRAB
MRAB

Reputation: 20664

You got what you wanted.

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

Upvotes: 1

Related Questions