如何将十六进制字符串转换为十六进制数字 [英] How to convert a hex string to hex number
本文介绍了如何将十六进制字符串转换为十六进制数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将十六进制字符串(例如: 0xAD4
)转换为十六进制数,然后将 0x200
添加到那个数字,又想以 0x
的形式打印该数字作为字符串。
我尝试了第一个步骤:
str(int(str(item [1] [: - 2]),16))
但是打印的值是十进制字符串而不是十六进制格式的字符串(采用0x格式)(我想以 0x
的形式打印最终结果)
-
[:-2]
从该号码删除最后一个00 -
item [1]
包含十六进制数字形式为0x
解决方案
<
hex_str =0xAD4
hex_int = int(hex_str,16)
new_int = hex_int + 0x200
print hex(new_int)
如果你不喜欢在开始时 0x
,将最后一行替换为
print hex(new_int)[2:]
I want to convert a hex string (ex: 0xAD4
) to hex number, then to add 0x200
to that number and again want to print that number in form of 0x
as a string.
i tried for the first step:
str(int(str(item[1][:-2]),16))
but the value that is getting printed is a decimal string not a hex formatted string (in 0x format) ( i want to print the final result in form of 0x
)
[:-2]
to remove the last 00 from that numberitem[1]
is containing hex number in form of0x
解决方案
Try this:
hex_str = "0xAD4"
hex_int = int(hex_str, 16)
new_int = hex_int + 0x200
print hex(new_int)
If you don't like the 0x
in the beginning, replace the last line with
print hex(new_int)[2:]
这篇关于如何将十六进制字符串转换为十六进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文