这将是史上最全的Python编码与进制转换
进制转换
特别注意:纯数字之间才可以进制转换
十进制->二进制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| In [3]: num_dec = 5 In [8]: type(num_dec) Out[8]: int
In [4]: num_bin = bin(num_dec)
In [5]: num_bin Out[5]: '0b101'
In [6]: type(num_bin) Out[6]: str
In [7]: len(num_bin) Out[7]: 5
|
十进制->八进制
1 2 3 4 5 6 7 8 9 10 11 12
| In [12]: num_dec = 9
In [13]: num_oct = oct(num_dec)
In [14]: num_oct Out[14]: '0o11'
In [15]: type(num_oct) Out[15]: str
In [16]: len(num_oct) Out[16]: 4
|
十进制->十六进制
1 2 3 4 5 6 7 8 9 10 11 12
| In [17]: num_dec = 25
In [18]: num_hex = hex(num_dec)
In [19]: num_hex Out[19]: '0x19'
In [20]: type(num_hex) Out[20]: str
In [21]: len(num_hex) Out[21]: 4
|
二进制->十进制
1 2 3 4 5 6 7 8 9 10 11 12
| In [30]: num_bin = 0b101 In [32]: type(num_bin) Out[32]: int
In [34]: num_dec = int(num_bin)
In [35]: num_dec Out[35]: 5
In [36]: type(num_dec) Out[36]: int
|
二进制->八进制
1 2 3 4 5 6 7 8 9 10 11 12 13
| In [41]: num_bin = 0b101
In [42]: num_oct = oct(num_bin)
In [43]: num_oct Out[43]: '0o5'
In [44]: type(num_oct) Out[44]: str
In [45]: len(num_oct) Out[45]: 3
|
二进制->十六进制
1 2 3 4 5 6 7 8 9 10 11 12
| In [46]: num_bin = 0b101
In [47]: num_hex = hex(num_bin)
In [48]: num_hex Out[48]: '0x5'
In [49]: type(num_hex) Out[49]: str
In [50]: len(num_hex) Out[50]: 3
|
八进制->十进制
1 2 3 4 5 6 7 8 9 10 11 12 13
| In [51]: num_oct = 0o101
In [52]: type(num_oct) Out[52]: int
In [53]: num_dec = int(num_oct)
In [54]: num_dec Out[54]: 65
In [55]: type(num_dec) Out[55]: int
|
八进制->二进制
1 2 3 4 5 6 7 8 9 10 11 12 13
| In [56]: num_oct = 0o101
In [57]: num_bin = bin(num_oct)
In [58]: num_bin Out[58]: '0b1000001'
In [59]: type(num_bin) Out[59]: str
In [60]: len(num_bin) Out[60]: 9
|
八进制->十六进制
1 2 3 4 5 6 7 8 9 10 11 12
| In [61]: num_oct = 0o101
In [62]: num_hex = hex(num_oct)
In [63]: num_hex Out[63]: '0x41'
In [64]: type(num_hex) Out[64]: str
In [65]: len(num_hex) Out[65]: 4
|
十六进制->十进制
1 2 3 4 5 6 7 8 9 10 11 12 13
| In [66]: num_hex = 0xff
In [67]: type(num_hex) Out[67]: int
In [68]: num_dec = int(num_hex)
In [69]: num_dec Out[69]: 255
In [70]: type(num_dec) Out[70]: int
|
十六进制->二进制
1 2 3 4 5 6 7 8 9 10 11 12 13
| In [71]: num_hex = 0xff
In [72]: num_bin = bin(num_hex)
In [73]: num_bin Out[73]: '0b11111111'
In [75]: type(num_bin) Out[75]: str
In [76]: len(num_bin) Out[76]: 10
|
十六进制->八进制
1 2 3 4 5 6 7 8 9 10 11 12
| In [77]: num_hex = 0xff
In [78]: num_oct = oct(num_hex)
In [79]: num_oct Out[79]: '0o377'
In [80]: type(num_oct) Out[80]: str
In [81]: len(num_oct) Out[81]: 5
|
类型转换
字符串(内容为十进制)->十进制数字
1 2 3 4 5 6 7 8 9 10 11 12
| In [100]: num_str = '100'
In [101]: type(num_str) Out[101]: str
In [103]: num_int = int(num_str)
In [104]: num_int Out[104]: 100
In [105]: type(num_int) Out[105]: int
|
字符串(内容为二进制)->十进制数字
1 2 3 4 5 6 7 8 9 10 11 12
| In [116]: num_str = '101001011'
In [117]: type(num_str) Out[117]: str
In [118]: num_int = int(num_str, 2)
In [119]: num_int Out[119]: 331
In [120]: type(num_int) Out[120]: int
|
字符串(内容为八进制)->十进制数字
1 2 3 4 5 6 7 8 9 10 11 12
| In [106]: num_str = '3751'
In [107]: type(num_str) Out[107]: str
In [108]: num_int = int(num_str, 8)
In [109]: num_int Out[109]: 2025
In [110]: type(num_int) Out[110]: int
|
字符串(内容为十六进制)->十进制数字
1 2 3 4 5 6 7 8 9 10 11 12
| In [111]: num_str = 'aabbccddff'
In [112]: type(num_str) Out[112]: str
In [113]: num_int = int(num_str, 16)
In [114]: num_int Out[114]: 733295205887
In [115]: type(num_int) Out[115]: int
|
字符串(内容为十六进制)->字节型(十六进制形式)
Python3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| In [121]: str_hex = 'aabbccddeeff'
In [122]: type(str_hex) Out[122]: str
In [123]: str_hex_true = bytes.fromhex(str_hex)
In [124]: str_hex_true Out[124]: b'\xaa\xbb\xcc\xdd\xee\xff'
In [125]: len(str_hex_true) Out[125]: 6
In [126]: type(str_hex_true) Out[126]: bytes
|
Python2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| In [4]: str_hex = 'aabbccddeeff'
In [5]: type(str_hex) Out[5]: str
In [6]: str_hex_true = str_hex.decode('hex')
In [7]: str_hex_true Out[7]: '\xaa\xbb\xcc\xdd\xee\xff'
In [8]: type(str_hex_true) Out[8]: str
In [9]: len(str_hex_true) Out[9]: 6
|
字节型(十六进制形式)->字符串(内容为十六进制)
Python3 1 2 3 4 5 6 7 8 9 10
| In [3]: str_hex_true Out[3]: b'\xaa\xbb\xcc\xdd\xee\xff'
In [4]: str_hex = str_hex_true.hex()
In [5]: str_hex Out[5]: 'aabbccddeeff'
In [6]: type(str_hex) Out[6]: str
|
Python2 1 2 3 4 5 6 7 8 9 10
| In [27]: str_hex_true Out[27]: '\xaa\xbb\xcc\xdd\xee\xff'
In [29]: str_hex = str_hex_true.encode('hex')
In [30]: str_hex Out[30]: 'aabbccddeeff'
In [31]: type(str_hex) Out[31]: str
|
编码转换
字符串->ASCII
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| In [7]: string_single = 'A'
In [8]: str2ascii = ord(string_single)
In [9]: str2ascii Out[9]: 65
In [10]: type(str2ascii) Out[10]: int
In [12]: string = 'I am MinuteSheep'
In [13]: str2ascii = ''.join(map(hex, map(ord, string))).replace('0x', '')
In [14]: str2ascii Out[14]: '4920616d204d696e7574655368656570'
In [15]: type(str2ascii) Out[15]: str
|
ASCII->字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| In [16]: ascii_single = 68
In [17]: ascii2str = chr(ascii_single)
In [18]: ascii2str Out[18]: 'D'
In [19]: type(ascii2str) Out[19]: str
In [26]: ascii = '4920616d204d696e7574655368656570'
In [27]: ascii2str = ''.join(map(chr, bytes.fromhex(ascii)))
In [28]: ascii2str Out[28]: 'I am MinuteSheep'
In [30]: type(ascii2str) Out[30]: str
|
Unicode、Utf-8、Gbk互转
不同编码格式之间的转换遵循的核心原则:Unicode可以编码为其它码,其它码可以解码为Unicode,因此不同的编码格式互相转换时需要Unicode作为中介
Python3所有的字符串默认为Unicode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
In [31]: str_unicode = '密码学'
In [32]: unicode2utf8 = str_unicode.encode('utf8')
In [33]: unicode2gbk = str_unicode.encode('gbk')
In [34]: str_unicode Out[34]: '密码学'
In [35]: unicode2utf8 Out[35]: b'\xe5\xaf\x86\xe7\xa0\x81\xe5\xad\xa6'
In [36]: unicode2gbk Out[36]: b'\xc3\xdc\xc2\xeb\xd1\xa7'
In [37]: utf8 = b'\xe5\xaf\x86\xe7\xa0\x81\xe5\xad\xa6' In [38]: gbk = b'\xc3\xdc\xc2\xeb\xd1\xa7'
In [39]: utf82unicode = utf8.decode('utf8')
In [40]: gbk2unicode = gbk.decode('gbk')
In [41]: utf82unicode Out[41]: '密码学'
In [42]: gbk2unicode Out[42]: '密码学'
|