Python 字符串與字節數組轉換


整數之間的進制轉換:

10進制轉16進制: hex(16)  ==>  0x10
16進制轉10進制: int('0x10', 16)  ==>  16

類似的還有oct(), bin()

字符串轉整數:

10進制字符串: int('10')  ==>  10
16進制字符串: int('10', 16)  ==>  16
16進制字符串: int('0x10', 16)  ==>  16

字節串轉整數:

轉義為short型整數: struct.unpack('<hh', bytes(b'\x01\x00\x00\x00'))  ==>  (1, 0)
轉義為long型整數: struct.unpack('<L', bytes(b'\x01\x00\x00\x00'))  ==>  (1,)

整數轉字節串:

轉為兩個字節: struct.pack('<HH', 1,2)  ==>  b'\x01\x00\x02\x00'
轉為四個字節: struct.pack('<LL', 1,2)  ==>  b'\x01\x00\x00\x00\x02\x00\x00\x00'

字符串轉字節串:

字符串編碼為字節碼: '12abc'.encode('ascii')  ==>  b'12abc'
數字或字符數組: bytes([1,2, ord('1'),ord('2')])  ==>  b'\x01\x0212'
16進制字符串: bytes().fromhex('010210')  ==>  b'\x01\x02\x10'
16進制字符串: bytes(map(ord, '\x01\x02\x31\x32'))  ==>  b'\x01\x0212'
16進制數組: bytes([0x01,0x02,0x31,0x32])  ==>  b'\x01\x0212'

字節串轉字符串:

字節碼解碼為字符串: bytes(b'\x31\x32\x61\x62').decode('ascii')  ==>  12ab
字節串轉16進制表示,夾帶ascii: str(bytes(b'\x01\x0212'))[2:-1]  ==>  \x01\x0212
字節串轉16進制表示,固定兩個字符表示: str(binascii.b2a_hex(b'\x01\x0212'))[2:-1]  ==>  01023132
字節串轉16進制數組: [hex(x) for x in bytes(b'\x01\x0212')]  ==>  ['0x1', '0x2', '0x31', '0x32']

案例總結

import binascii  
import struct  

def example(express, result=None):  
    if result == None:  
        result = eval(express)  
    print(express, ' ==> ', result)  

if __name__ == '__main__':  
      
    print('整數之間的進制轉換:')  
    print("10進制轉16進制", end=': ');example("hex(16)")  
    print("16進制轉10進制", end=': ');example("int('0x10', 16)")  
    print("類似的還有oct(), bin()")  
      
    print('\n-------------------\n')  
      
    print('字符串轉整數:')  
    print("10進制字符串", end=": ");example("int('10')")  
    print("16進制字符串", end=": ");example("int('10', 16)")  
    print("16進制字符串", end=": ");example("int('0x10', 16)")  
      
    print('\n-------------------\n')  
      
    print('字節串轉整數:')  
    print("轉義為short型整數", end=": ");example(r"struct.unpack('<hh', bytes(b'\x01\x00\x00\x00'))")  
    print("轉義為long型整數", end=": ");example(r"struct.unpack('<L', bytes(b'\x01\x00\x00\x00'))")  
  
    print('\n-------------------\n')  
  
    print('整數轉字節串:')  
    print("轉為兩個字節", end=": ");example("struct.pack('<HH', 1,2)")  
    print("轉為四個字節", end=": ");example("struct.pack('<LL', 1,2)")  
      
    print('\n-------------------\n')  
      
    print('字符串轉字節串:')  
    print('字符串編碼為字節碼', end=": ");example(r"'12abc'.encode('ascii')")  
    print('數字或字符數組', end=": ");example(r"bytes([1,2, ord('1'),ord('2')])")  
    print('16進制字符串', end=': ');example(r"bytes().fromhex('010210')")  
    print('16進制字符串', end=': ');example(r"bytes(map(ord, '\x01\x02\x31\x32'))")  
    print('16進制數組', end =': ');example(r'bytes([0x01,0x02,0x31,0x32])')  
      
    print('\n-------------------\n')  
      
    print('字節串轉字符串:')  
    print('字節碼解碼為字符串', end=": ");example(r"bytes(b'\x31\x32\x61\x62').decode('ascii')")  
    print('字節串轉16進制表示,夾帶ascii', end=": ");example(r"str(bytes(b'\x01\x0212'))[2:-1]")  
    print('字節串轉16進制表示,固定兩個字符表示', end=": ");example(r"str(binascii.b2a_hex(b'\x01\x0212'))[2:-1]")  
    print('字節串轉16進制數組', end=": ");example(r"[hex(x) for x in bytes(b'\x01\x0212')]")  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM