简体   繁体   English

Python 3.3:十六进制和十进制转换

[英]Python 3.3: Hex and Dec converting

I'm able to convert Hex data to Decimal with: 我可以使用以下方式将十六进制数据转换为十进制:

file = open("my_text.txt", "r+")
data = input("Type Hex: ")
hex = int(data, 16)
str(hex)
print(str(hex))
file.write(str(hex))
file.close()
input("close: ")

But how can I convert Decimal data, like a number or a sentence, to Hex? 但是,如何将十进制数据(例如数字或句子)转换为十六进制? Also, is it possible to write data to a hexadecimal offset? 另外,是否可以将数据写入十六进制偏移量?

How about something like this? 这样的事情怎么样?

>>> print(hex(257))
0x101
>>> for ch in b'abc':
...    print(hex(ch))
...
0x61
0x62
0x63

BTW, assigning to a variable called "hex" occludes the built-in function - it's best to avoid that. 顺便说一句,分配给名为“ hex”的变量会阻塞内置函数-最好避免这种情况。

HTH 高温超导

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

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