简体   繁体   中英

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.

HTH

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