Question

I am doing data transfer from an instrument over RS232 serial dialogue using Python. The data is coming in ASCII format. For example this is what i get in ASCII from the instrument using Python:

'\x02S20390908127F010102F3004000900300000000000000000000000000000300A500000000000202020202020206070505050505050000000707070707070707020700000000000000000000000000000000000000008000800089237A0715047E000000000000000000000000000000000000000000000000000000000000000000000000005350374646303030\x1732'

The same data transfer using Hyperterminal (Docklight) gave me this and the following is the hex representation i got in hyperterminal:

02 53 31 38 33 38 30 39 30 38 31 32 37 46 30 31 30 31 30 32 46 33 30 30 34 30 30 30 39 30 30 33 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 33 30 30 41 35 30 30 30 30 30 30 30 30 30 30 30 32 30 32 30 32 30 32 30 32 30 32 30 32 30 36 30 37 30 35 30 35 30 35 30 35 30 35 30 35 30 30 30 30 30 30 30 37 30 37 30 37 30 37 30 37 30 37 30 37 30 37 30 32 30 37 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 38 30 30 30 38 30 30 30 38 39 32 33 37 41 30 37 31 35 30 34 37 45 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 35 33 35 30 33 37 34 36 34 36 33 30 33 30 33 30 17 33 38 

My question is that how do I, using Python, get the same hex representation from the ascii data I got. I am very much of a noob when it comes to this kind of stuff. Thank you in advance.

Was it helpful?

Solution

Explanation: convert each character into byte using ord then format as two digit hex number and feed it into a list. Then join the list using space as a separator.

>>> ll = ["%02X" % (ord(x)) for x in '\x02S20390908127F010102F3004000900300000000000000000000000000000300A500000000000202020202020206070505050505050000000707070707070707020700000000000000000000000000000000000000008000800089237A0715047E000000000000000000000000000000000000000000000000000000000000000000000000005350374646303030\x1732']
>>> ll           #as a list
['02', '53', '32', '30', '33', '39', '30', '39', '30', '38', '31', '32', '37', '46', '30', '31', '30', '31', '30', '32', '46', '33', '30', '30', '34', '30', '30', '30', '39', '30', '30', '33', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '33', '30', '30', '41', '35', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '32', '30', '32', '30', '32', '30', '32', '30', '32', '30', '32', '30', '32', '30', '36', '30', '37', '30', '35', '30', '35', '30', '35', '30', '35', '30', '35', '30', '35', '30', '30', '30', '30', '30', '30', '30', '37', '30', '37', '30', '37', '30', '37', '30', '37', '30', '37', '30', '37', '30', '37', '30', '32', '30', '37', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '38', '30', '30', '30', '38', '30', '30', '30', '38', '39', '32', '33', '37', '41', '30', '37', '31', '35', '30', '34', '37', '45', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '30', '35', '33', '35', '30', '33', '37', '34', '36', '34', '36', '33', '30', '33', '30', '33', '30', '17', '33', '32']
>>> ' '.join(ll) # as a string
'02 53 32 30 33 39 30 39 30 38 31 32 37 46 30 31 30 31 30 32 46 33 30 30 34 30 30 30 39 30 30 33 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 33 30 30 41 35 30 30 30 30 30 30 30 30 30 30 30 32 30 32 30 32 30 32 30 32 30 32 30 32 30 36 30 37 30 35 30 35 30 35 30 35 30 35 30 35 30 30 30 30 30 30 30 37 30 37 30 37 30 37 30 37 30 37 30 37 30 37 30 32 30 37 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 38 30 30 30 38 30 30 30 38 39 32 33 37 41 30 37 31 35 30 34 37 45 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 35 33 35 30 33 37 34 36 34 36 33 30 33 30 33 30 17 33 32'

OTHER TIPS

This seems to be the easiest way...

ascstr = 'x\02S20..."  #your string

#I think you need to get rid of the first 4 characters '\x02' looks like
#it is hex
ascstr = ascstr[4:] 
for c in mystring:
   hexstr = hexstr + hex(ord(c))[2:] + ' '
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top