简体   繁体   English

转换字符串:十六进制>十进制和解析

[英]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 字符串如下所示: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 我一直在使用此网站,但始终要一一进行是非常繁琐的任务: 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]) 我需要python,我正在用bytearray数据包发送来编码一些东西,看起来像这样: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 此时,您可以打印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 或将其保存到文件等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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