[英]python converting hexadecimal binary to string
我正在使用python3.5
,我希望將以十六進制字節( b'\\x00'
, b'\\x01'
等)獲得的輸出寫入python字符串中,並帶有\\x00 -> 0
和\\x01 -> 1
,我有這個感覺它可以很容易地以Python的方式完成,但是半小時的谷歌搜索仍然使我認為最簡單的方法是手工制作帶有映射的字典(我實際上只需要0到7)。
Input Intended output
b'\x00' 0 or '0'
b'\x01' 1 or '1'
等等
不確定是否要此結果,但是請嘗試
output = [str(ord(x)) for x in output]
字節字符串自動是數字列表。
input_bytes = b"\x00\x01"
output_numbers = list(input_bytes)
您只是在尋找這樣的東西嗎?
for x in range(0,8):
(x).to_bytes(1, byteorder='big')
輸出為:
b'\x00'
b'\x01'
b'\x02'
b'\x03'
b'\x04'
b'\x05'
b'\x06'
b'\x07'
或相反:
byteslist = [b'\x00',
b'\x01',
b'\x02',
b'\x03',
b'\x04',
b'\x05',
b'\x06',
b'\x07']
for x in byteslist:
int.from_bytes(x,byteorder='big')
輸出:
0
1
2
3
4
5
6
7
如果需要將b"\\x0F"
轉換為F
使用
print( hex(ord(b'\x0F'))[2:] )
或使用format()
print( format(ord(b'\x0F'), 'X') ) # '02X' gives string '0F'
print( '{:X}'.format(ord(b'\x0F')) ) # '{:02X}' gives string '0F'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.