pythonnetwork-programmingencodingbyteutf-16le

Python network convert byte


I'm trying to implement a CRC verification on a network based protocol. CRC calculation is done via the PyCRC lib.

PyCRC will generate a checksum for the given packet and return a result like this: CB3D9FD1

When I try to send it on the wire, somehow python modify my string and convert it in ascii to Hex, example:

Crc = "CB3D9FD1"
...
buffer = "\x41\x42\x43\x44"+crc
...
s.send(buffer)

On the wire I will see that:

\x41\x42\x43\x44***\x43\x42\x33\x44\x39\x46\x44\x31***

Instead of:

 \x41\x42\x43\x44***\xCB\x3D\x9F\xD1***

Any ways to solve this issue easily ?

Thanks !


Solution

  • Actually, don't convert your CRC to ASCII. Keep it in hex and then pack it into a byte string using struct.pack::

    buffer += struct.pack("L", crc)