蘋(píng)果手機(jī)統(tǒng)計(jì)流量查看 蘋(píng)果如何看流量統(tǒng)計(jì)
2024-07-03
0x1016進(jìn)制轉(zhuǎn)1" />
更新時(shí)間:2024-07-03 00:03:51作者:未知
進(jìn)行協(xié)議解析時(shí),總是會(huì)遇到各種各樣的數(shù)據(jù)轉(zhuǎn)換的問(wèn)題,從二進(jìn)制到十進(jìn)制,從字節(jié)串到整數(shù)等等
廢話不多上,直接上例子
整數(shù)之間的進(jìn)制轉(zhuǎn)換:
10進(jìn)制轉(zhuǎn)16進(jìn)制: hex(16) ==> 0x10
16進(jìn)制轉(zhuǎn)10進(jìn)制: int(‘0x10’, 16) ==> 16
類(lèi)似的還有oct(), bin()
——————-
字符串轉(zhuǎn)整數(shù):
10進(jìn)制字符串: int(’10’) ==> 10
16進(jìn)制字符串: int(’10’, 16) ==> 16
16進(jìn)制字符串: int(‘0x10’, 16) ==> 16
——————-
字節(jié)串轉(zhuǎn)整數(shù):
轉(zhuǎn)義為short型整數(shù): struct.unpack(‘<hh’, bytes(b’x01x00x00x00′)) ==> (1, 0)
轉(zhuǎn)義為long型整數(shù): struct.unpack(‘<L’, bytes(b’x01x00x00x00′)) ==> (1,)
——————-
整數(shù)轉(zhuǎn)字節(jié)串:
轉(zhuǎn)為兩個(gè)字節(jié): struct.pack(‘<HH’, 1,2) ==> b’x01x00x02x00′
轉(zhuǎn)為四個(gè)字節(jié): struct.pack(‘<LL’, 1,2) ==> b’x01x00x00x00x02x00x00x00′
——————-
字符串轉(zhuǎn)字節(jié)串:
字符串編碼為字節(jié)碼: ’12abc’.encode(‘a(chǎn)scii’) ==> b’12abc’
數(shù)字或字符數(shù)組: bytes([1,2, ord(‘1’),ord(‘2′)]) ==> b’x01x0212’
16進(jìn)制字符串: bytes().fromhex(‘010210′) ==> b’x01x02x10’
16進(jìn)制字符串: bytes(map(ord, ‘x01x02x31x32′)) ==> b’x01x0212’
16進(jìn)制數(shù)組: bytes([0x01,0x02,0x31,0x32]) ==> b’x01x0212′
——————-
字節(jié)串轉(zhuǎn)字符串:
字節(jié)碼解碼為字符串: bytes(b’x31x32x61x62′).decode(‘a(chǎn)scii’) ==> 12ab
字節(jié)串轉(zhuǎn)16進(jìn)制表示,夾帶ascii: str(bytes(b’x01x0212′))[2:-1] ==> x01x0212
字節(jié)串轉(zhuǎn)16進(jìn)制表示,固定兩個(gè)字符表示: str(binascii.b2a_hex(b’x01x0212′))[2:-1] ==> 01023132
字節(jié)串轉(zhuǎn)16進(jìn)制數(shù)組: [hex(x) for x in bytes(b’x01x0212′)] ==> [‘0x1’, ‘0x2’, ‘0x31’, ‘0x32’]
===================
測(cè)試用的python源碼