I know this is a basic question, but I am really struggling to work it out.
I am trying to get a string to convert to FF in hex (i.e. OxFF) in python 2.7.
I convert the string to bytearray then to hex using the following code:
>>> data = 'OxFF'
>>> array = bytearray(data)
>>> print binascii.hexlify(array)
4f784646
For context, I am sending a message over TCP/IP using twisted and the header needs to be FF FF
Any help would be amazing.
Cheers
You can create directly the header as a string literal:
>>> header = '\xff\xff'
>>> hedaer
'\xff\xff'
Converting from data
:
>>> data = '0xFF'
>>> binascii.unhexlify(data[2:])
'\xff'
The key is to skip over the 0x
prefix.
Or as an integer:
>>> int(data, 16)
255
where 255 is 0xFF of course.