I'm trying to confirm this struct is being formatted correctly for network byte order, but printing out the bytes and printing out the hex of the bytes give me different output, with the hex being the expected output. But I don't know why they are different.
import ctypes
class test_struct(ctypes.BigEndianStructure):
_pack_ = 1
_fields_ = [ ('f1', ctypes.c_ushort, 16) ]
foo = test_struct()
foo.f1 = 0x8043
bs = bytes(foo)
print(str(bs[0:1]) + " " + str(bs[1:2]))
print(bs.hex(' '))
The output is
b'\x80' b'C'
80 43
Cis0x43in ASCII.print()needs to be changed to print hex output. Try using this:print("{0:02x} {1:02x}".format(bs[0],bs[1]))instead ofprint(str(bs[0:1]) + " " + str(bs[1:2])). Check this thread for more options: stackoverflow.com/q/16572008/26993270