1. 程式人生 > >python常用的十進位制、16進位制、字串、位元組串之間的轉換

python常用的十進位制、16進位制、字串、位元組串之間的轉換

整數之間的進位制轉換:

  • 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']


===================

測試用的python原始碼

  1. ''''' 
  2. Created on 2014年8月21日 
  3. @author: lenovo 
  4. '''
  5. import binascii  
  6. import struct  
  7. def example(express, result=None):  
  8.     if result == None:  
  9.         result = eval(express)  
  10.     print(express, ' ==> ', result)  
  11. if __name__ == '__main__':  
  12.     print('整數之間的進位制轉換:')  
  13.     print("10進位制轉16進位制", end=': ');example("hex(16)")  
  14.     print("16進位制轉10進位制", end=': ');example("int('0x10', 16)")  
  15.     print("類似的還有oct(), bin()")  
  16.     print('\n-------------------\n')  
  17.     print('字串轉整數:')  
  18.     print("10進位制字串", end=": ");example("int('10')")  
  19.     print("16進位制字串", end=": ");example("int('10', 16)")  
  20.     print("16進位制字串", end=": ");example("int('0x10', 16)")  
  21.     print('\n-------------------\n')  
  22.     print('位元組串轉整數:')  
  23.     print("轉義為short型整數", end=": ");example(r"struct.unpack('<hh', bytes(b'\x01\x00\x00\x00'))")  
  24.     print("轉義為long型整數", end=": ");example(r"struct.unpack('<L', bytes(b'\x01\x00\x00\x00'))")  
  25.     print('\n-------------------\n')  
  26.     print('整數轉位元組串:')  
  27.     print("轉為兩個位元組", end=": ");example("struct.pack('<HH', 1,2)")  
  28.     print("轉為四個位元組", end=": ");example("struct.pack('<LL', 1,2)")  
  29.     print('\n-------------------\n')  
  30.     print('字串轉位元組串:')  
  31.     print('字串編碼為位元組碼', end=": ");example(r"'12abc'.encode('ascii')")  
  32.     print('數字或字元陣列', end=": ");example(r"bytes([1,2, ord('1'),ord('2')])")  
  33.     print('16進位制字串', end=': ');example(r"bytes().fromhex('010210')")  
  34.     print('16進位制字串', end=': ');example(r"bytes(map(ord, '\x01\x02\x31\x32'))")  
  35.     print('16進位制陣列', end =': ');example(r'bytes([0x01,0x02,0x31,0x32])')  
  36.     print('\n-------------------\n')  
  37.     print('位元組串轉字串:')  
  38.     print('位元組碼解碼為字串', end=": ");example(r"bytes(b'\x31\x32\x61\x62').decode('ascii')")  
  39.     print('位元組串轉16進製表示,夾帶ascii', end=": ");example(r"str(bytes(b'\x01\x0212'))[2:-1]")  
  40.     print('位元組串轉16進製表示,固定兩個字元表示', end=": ");example(r"str(binascii.b2a_hex(b'\x01\x0212'))[2:-1]")  
  41.     print('位元組串轉16進位制陣列', end=": ");example(r"[hex(x) for x in bytes(b'\x01\x0212')]")  
  42.     print('\n===================\n')  
  43.     print("以上原理都比較簡單,看一下就明白了。這裡僅僅是拋磚引玉,有更好更簡單的方法,歡迎歡迎")