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出來就可以了。

感覺寫得很囉嗦,不知道有沒有更簡單的處理方法。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章