Python 3.4: Converting ushort to bytes

911 views Asked by At

I'm trying to convert a ushort to bytes. However, when I try this:

>>import struct
>>val =struct.pack('<H',10000)
b"\x10'"

Instead of:

b'\x10\x27'

Is this a bug? Or I am just doing something silly?

I'll be writing this data to a serial device.

Thanks in advance.

1

There are 1 answers

1
Moses Koledoye On BEST ANSWER

It's an alternate representation for \x27:

>>> hex(ord("'"))
'0x27'

You won't have any problems converting back to the int representation:

>>> int.from_bytes(b"\x10'", 'little')
10000