简体   繁体   English

如何将十进制字符串更改为十六进制格式的字符串?

[英]How do I change a decimal string to a hex formatted string?

How do I change a decimal string like 123456789 to a hex formatted string like this: 如何将十进制字符串(如123456789更改为十六进制格式的字符串,如下所示:

07:5B:CD:15

I have looked at the hex function, but it doesn't give me the above format. 我已经看过hex函数,但是它没有给出上述格式。

UPDATED 更新

I would also like -123456789 to be: 我也希望-123456789是:

-07:5B:CD:15 

rather than 而不是

-7:5B:CD:15
s = '123456789'
a = '{:08X}'.format(int(s))
print ':'.join(a[i:i+2] for i in xrange(0, len(a), 2))

prints 版画

07:5B:CD:15

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

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