I'm working on a project where I receive data from an XBEE radio. The data comes in different strings... I've finally found a way to extract the actual information however, I really think there has got to be a better way to convert the data into usable form.
Here is an example of a frame:
{'analog_ch_mask': '\x18', 'first_sample': '\x03Q', 'number_samples': '\x01',
'options': '\xc1', 'addr_short': '\xff\xfe',
'address_low': '@c{!', 'digital_ch_mask': '\x00\x00',
'address_high': '\x00\x13\xa2\x00', 'second_sample': '\x00\xca', 'id': 'io_sample_rx'}
The issue I've encountered is the formatting of the data, the following works for me.
# Extract the characters and join them together.
sample_data = ''.join("{:02X}".format(ord(c)) for c in item['key'])
print(sample_data)
print type(sample_data) # is a string
# Convert the hex string into an integer
sample_data = int(sample_data, 16)
print(sample_data)
print type(sample_data) # is an integer
# Try converting to hex string just for fun
sample_data = hex(sample_data)
print(sample_data)
print type(sample_data) # is a string
I like that this works for both the ascii data as well as the escape hex strings. However, shouldn't there be a more direct way to do these operations? I attempted using unpack, but I was getting errors.
Cheers.