python經常使用的十進位、16進位、字串、位元組串之間的轉換(長期更新帖)

來源:互聯網
上載者:User

標籤:進位   ascii   decode   class   content   rom   encode   code   pac   

進行協議解析時。總是會遇到各種各樣的資料轉換的問題,從二進位到十進位,從位元組串到整數等等

廢話不多上。直接上範例


整數之間的進位轉換:

  • 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原始碼

‘‘‘Created on 2014年8月21日@author: lenovo‘‘‘import binasciiimport structdef 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‘)]")            print(‘\n===================\n‘)    print("以上原理都比較簡單。看一下就明確了。

這裡不過拋磚引玉,有更好更簡單的方法,歡迎歡迎")


以上原理都比較簡單,看一下就明確了。

這裡不過拋磚引玉,有更好更簡單的方法,歡迎歡迎

python經常使用的十進位、16進位、字串、位元組串之間的轉換(長期更新帖)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.