2

I'm trying to send some bytes from Python. I'm using struct.pack to get the corresponding sending bytes. For example:

>>> struct.pack('BBBBBh', 200, 0, 56, 6, 0, 6)
'\xc8\x008\x06\x00\x00\x06\x00'

If you take a close look, you can see that the second byte (that sould be \x00) has been merged with the next byte (that should be \x38).

In fact, if I try to concatenate by text:

>>> '\x00' + '\x38'
'\x008'

The same thing happens.

I need to have '\x00\x38'. Is this possible? Am I missing something related to formats or whatsoever?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

1

If a byte is a printable character, Python will print it instead of its hexadecimal value.

0x38 (decimal 56) is the ASCII code for the character '8', therefore '\x00\x38' is printed as '\x008'.

If you want to print the hexadecimal values for all bytes, you have to do it explicitly, for example:

print(['{:02x}'.format(b) for b in data])
mkrieger1
  • 19,194
  • 5
  • 54
  • 65