網站首頁 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
相關推薦
- 2023-12-21 redis簡介和配置教程
- 2022-02-13 自動化進行Pod的擴縮容-HPA
- 2023-02-04 詳解如何在C#中接受或拒絕Excel中的修訂_C#教程
- 2023-04-17 Python屬性私有化詳解_python
- 2022-11-16 Docker如何安全地停止和刪除容器_docker
- 2023-10-18 下載文件時前端重命名的實現方法將url地址轉化為文件實現重命名
- 2022-12-30 解決React報錯useNavigate()?may?be?used?only?in?context
- 2022-09-05 詳解apache編譯安裝httpd-2.4.54及三種風格的init程序特點和區別_Linux
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支