Search code examples
pythonpython-3.xbyteuint16ushort

Python 3.4: Converting ushort to bytes


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.


Solution

  • 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