I am using Struct to consume a binary file containing raw sensor data. The data is a series of 16-bit values. If the data contains an ASCII code such as 0x63 it prints as the ASCII letter 'c'. The letter 'c' means nothing to me when I'm browsing my sensor data. How can I force this data to print in hex notation?
Here is what happens using the normal print built-in:
>>> from construct import *
>>> data = "\x01\x02\x63\x64"
>>> s = Struct("s", Field("val1",2), Field("val2",2))
>>> print s.parse(data)
Container:
val1 = '\x01\x02'
val2 = 'cd'
The output I would like is something like:
>>> print s.parse(data)
Container:
val1 = 0x0102
val2 = 0x6364
Any clever ideas on how to do this? Thanks!
p.s
The monkey-wrench thrown in is that this example simplifies my Struct greatly. I have other data in this struct that isn't sensor data (timestamp, etc) and I am happy with how that prints out by default. For example, a timestamp of ("12:30:05") would not be useful to view as 0x313233303035.