简体   繁体   中英

Converting a string: Hexadecimal > Decimal & Parsing

Anyone can help me with a solution to convert a whole string from hexadecimal to decimal?

The string looks like this: 0e:e9:6e:00:31:2e:36:2e:38:4d:61:6a:6f:72:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:0b:32:31:45:39:33:35:42:41:44:37:30:00

I've been using this site, but it's really tedious tasks to go one by one all the time: http://www.binaryhexconverter.com/hex-to-decimal-converter

I need it for python, I'm coding some stuff with bytearray packet sending and it looks like this: my_bytes = bytearray([14,233,110,0,49,46,54,46,56,77,97,106,111,114,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,50,49,69,57,51,53,66,65,68,55,48,0])

So it would be the ideal to parse into that format somehow. Thanks for your attention! :)

you can try something like this:

input = '0e:e9:6e:00:31:2e:36:2e:38:4d:61:6a:6f:72:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:0b:32:31:45:39:33:35:42:41:44:37:30:00'

parsed = input.split(':')

outstring = ','.join(str(int(val, 16)) for val in parsed)

my_bytes = bytearray(outstring)

at this point you can print my_bytes

print my_bytes

14,233,110,0,49,46,54,46,56,77,97,106,111,114,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,50,49,69,57,51,53,66,65,68,55,48,0

or save it to a file, etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM