日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 Python教程 正文

Python如何讀取16進制byte數據_python

作者:rysander ? 更新時間: 2022-07-17 Python教程

如何讀取16進制byte數據

小弟最近在做網絡編程的時候,遇到了一些byte數據需要儲存,但是不是常見的str字符對應的byte,類似于b'\x00\xff\xfe\x01'這樣的數據,查找資料后發現這種東西是16進制編碼的byte格式,可以直接轉成str沒有問題,但是再轉回bytes就會出現莫名其妙的雙斜杠,很是頭疼。

a = b'\x00\xef\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x19\x39\xd9\x9d\xfdABCDabcd'
b = str(a)
?
print(b)
>>> b'\x00\xef\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x199\xd9\x9d\xfdABCDabcd'
?
print(bytes(b,'utf8'))
>>> b"b'\\x00\\xef\\xa2\\xa0\\xb3\\x8b\\x9d\\x1e\\xf8\\x98\\x199\\xd9\\x9d\\xfdABCDabcd'"

嘗試寫入文件,再讀取也是如此,因為寫進去的形式就是str字符

# 寫入data.txt
a = b'\x00\xef\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x19\x39\xd9\x9d\xfdABCDabcd'
with open('data.txt','w') as p:
? ? p.write(str(a))
?
# 讀取data.txt
with open('data.txt','r') as p:
? ? line = p.readline()
?
print(line, type(line) == str)
>>> b'\x00\xef\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x199\xd9\x9d\xfdABCDabcd\\' True
?
print(bytes(line,'utf8'))
>>> b"b'\\x00\\xef\\xa2\\xa0\\xb3\\x8b\\x9d\\x1e\\xf8\\x98\\x199\\xd9\\x9d\\xfdABCDabcd\\\\'"

觀察了一下ASCII碼,發現主要還是因為\x字符被理解成了一個斜杠加x的形式,然后被儲存為str形式,相當于變成了兩個字節。

這樣解碼的時候分開解了,但是\xnn這種形式是應該看作ASCII碼的,于是我寫了個轉義的邏輯進行讀取:

def readbytetxt(filename):
? ? dic = {
? ? '0': 0, ? ?'1': 1, ? ?'2': 2,
? ? '3': 3, ? ?'4': 4, ? ?'5': 5,
? ? '6': 6, ? ?'7': 7, ? ?'8': 8,
? ? '9': 9, ? ?'a': 10, ? 'b': 11,
? ? 'c': 12, ? 'd': 13, ? 'e': 14,
? ? 'f': 15,
? ? }
? ? with open(filename,'r') as p:
? ? ? ? line = p.readline()
? ? ? ? while line:
? ? ? ? ? ? if line[-1] == '\n':
? ? ? ? ? ? ? ? line = line[:-1]
? ? ? ? ? ? i = 2
? ? ? ? ? ? L = b''
? ? ? ? ? ? while i+1 < len(line):
? ? ? ? ? ? ? ? if line[i:i+2] == '\\x' and (line[i+2] in dic.keys()) and (line[i+3] in dic.keys()):
? ? ? ? ? ? ? ? ? ? L += bytes([dic[line[i+2]]*16+dic[line[i+3]]])
? ? ? ? ? ? ? ? ? ? i += 4
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? L += bytes(line[i],'utf8')
? ? ? ? ? ? ? ? ? ? i += 1
? ? ? ? ? ? return L
? ? ? ? ? ? line = p.readline()
?
print(readbytetxt('data.txt'))
>>> b'\x00\xef\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x19\x39\xd9\x9d\xfdABCDabcd'

問題解決了!基本就是寫了個遍歷,然后遇到\x就把16進制轉成十進制的int,然后解碼成bytes,這樣常見的十六進制格式基本都能調用了。

后來發現除了\x還有其他的轉義字符,比如\\,\n,如果不添加轉變邏輯的話,依然會出現不識別的問題,于是重寫了一下函數,支持了常見的大部分轉義字符,并且寫成了生成器輸出。

def readbytetxt2(filename):
? ? dic = {
? ? '0': 0, ? ?'1': 1, ? ?'2': 2,
? ? '3': 3, ? ?'4': 4, ? ?'5': 5,
? ? '6': 6, ? ?'7': 7, ? ?'8': 8,
? ? '9': 9, ? ?'a': 10, ? 'b': 11,
? ? 'c': 12, ? 'd': 13, ? 'e': 14,
? ? 'f': 15,
? ? }
? ? dic2 = {
? ? 'a': '\a', ? ? 'b': '\b',?
? ? 'f': '\f', ? ? 'n': '\n',?
? ? 'r': '\r', ? ? 'v': '\v',?
? ? '\'': '\'', ? ?'\"': '',?
? ? '\\': '\\',?
? ? }
? ? with open(filename,'r') as p:
? ? ? ? line = p.readline()
? ? ? ? while line:
? ? ? ? ? ? if line[-1] == '\n':
? ? ? ? ? ? ? ? line = line[:-1]
? ? ? ? ? ? i = 2
? ? ? ? ? ? L = b''
? ? ? ? ? ? while i+1 < len(line):
? ? ? ? ? ? ? ? if line[i:i+2] == '\\x' and (line[i+2] in dic.keys()) and (line[i+3] in dic.keys()):
? ? ? ? ? ? ? ? ? ? L += bytes([dic[line[i+2]]*16+dic[line[i+3]]])
? ? ? ? ? ? ? ? ? ? i += 4
? ? ? ? ? ? ? ? elif line[i] == '\\' and line[i+1] in dic2.keys():
? ? ? ? ? ? ? ? ? ? L += bytes(dic2[line[i+1]],'utf8')
? ? ? ? ? ? ? ? ? ? i += 2
? ? ? ? ? ? ? ? elif line[i:i+4] == '\\000':
? ? ? ? ? ? ? ? ? ? L += bytes('\000','utf8')
? ? ? ? ? ? ? ? ? ? i += 2
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? L += bytes(line[i],'utf8')
? ? ? ? ? ? ? ? ? ? i += 1
? ? ? ? ? ? yield L
? ? ? ? ? ? line = p.readline()
?
a = b'\x00\xef\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x19\x39\xd9\x9d\xfdthe first line\n\r\a\b\t\\\f\'\"\v\b\n\000'
b = b'\xa0\xdf\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x19\x39\xd9\x9d\xfdthe second line\nn'
c = b'\xe0\xaf\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x19\x39\xd9\x9d\xfdthe third line\\'
with open('data.txt','w') as p:
? ? p.write(str(a)+'\n')
? ? p.write(str(b)+'\n')
? ? p.write(str(c))
?
line = readbytetxt2('data.txt')
?
print([a for a in line])
>>> [b'\x00\xef\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x199\xd9\x9d\xfdthe first line\n\r\x07\x08\\t\\\x0c\'"\x0b\x08\n\x00', b'\xa0\xdf\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x199\xd9\x9d\xfdthe second line\nn', b'\xe0\xaf\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x199\xd9\x9d\xfdthe third line\\']

基本上至此為止,大部分編碼形式都可以搞定了。

但是。。。其實還有一個更簡單的方式!因為其實萬惡之源就是str字符格式里面有很多轉義的地方不清不楚的,我想要的是byte存進文件,再以byte讀出來,而byte格式本來就是16進制的數字,說到底其實只要能存數字就可以了!所以寫了個更簡單的方法,直接轉成數字存數字列表就好!

L = []
a = b'\x00\xef\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x19\x39\xd9\x9d\xfdthe first line\n\r\a\b\t\\\f\'\"\v\b\n\000'
print(a)
for each in a:
? ? L.append(int(each))
with open('data.txt','w') as p:
? ? p.write(str(L))
print(L)
>>> [0, 239, 162, 160, 179, 139, 157, 30, 248, 152, 25, 57, 217, 157, 253, 116, 104, 101, 32, 102, 105, 114, 115, 116, 32, 108, 105, 110, 101, 10, 13, 7, 8, 9, 92, 12, 39, 34, 11, 8, 10, 0]
?
?
with open('data.txt','r') as p:
? ? line = p.readline()
print(b''.join([bytes([int(i)]) for i in line[1:-1].split(',')]))
>>> b'\x00\xef\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x199\xd9\x9d\xfdthe first line\n\r\x07\x08\t\\\x0c\'"\x0b\x08\n\x00'

存進去的是數字列表,然后用split的方式讀出來就可以了,這樣也不會有各種轉義搞不清的地方,數字是什么就讀什么byte出來就可以了。

Python的十六進制數

轉換關系

十進制整數轉十六進制整數用hex();十六進制整數轉十進制整數用int()

類似地,十進制整數轉二進制整數用bin();十進制整數轉八進制整數用oct()

hex() 函數

描述:hex() 函數用于將10進制整數轉換成16進制,以字符串形式表示。

語法:

  • hex(x)

參數說明:x – 10進制整數

返回值:返回16進制數,以字符串形式表示。

int() 函數

描述:int() 函數用于將一個字符串或數字轉換為整型。

語法:

  • class int(x, base=10)

參數說明:x – 字符串或數字。base – 進制數,默認十進制。

返回值:返回整型數據。

運算

對于十六進制整數,在進行運算前先轉換成十進制整數,再對其進行運算,之后將運算結果轉換回十六進制數。

原文鏈接:https://blog.csdn.net/weixin_40222586/article/details/102945030

欄目分類
最近更新