samsara
samsara

Reputation: 13

Printing hex string

print 'Payload: ', struct.unpack('%dB'%len(payload), payload)
print '%08x (%d bits) DATA: ' % (identifier, width), repr(payload)

These two code statements generate the following results:

Payload: (125, 255, 255, 125, 255, 255, 125, 255)
18feef00 (29 bits) DATA: '}\xff\xff}\xff\xff}\xff'

I'd like to have ONE final string that has the correct hex data that looks like this:

7dffff7dffff7dff

Upvotes: 1

Views: 7245

Answers (1)

Daniel DiPaolo
Daniel DiPaolo

Reputation: 56418

>>> tup = (125, 255, 255, 125, 255, 255, 125, 255)
>>> "".join('%02x' % i for i in tup)
'7dffff7dffff7dff'

So, in your case, you can use struct.unpack to construct the tuple and then use the "".join() to construct the string.

It's unclear what you are starting with, but if it's the string '}\xff\xff}\xff\xff}\xff' then this works without the tuple:

>>> s = '}\xff\xff}\xff\xff}\xff'
>>> "".join('%02x' % ord(c) for c in s)
'7dffff7dffff7dff'

Upvotes: 3

Related Questions