Python的功能模块[3] - binascii - 编码转换
生活随笔
收集整理的這篇文章主要介紹了
Python的功能模块[3] - binascii - 编码转换
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
binascii模塊 / binascii Module
binascii模塊包含很多在二進(jìn)制和 ASCII 編碼的二進(jìn)制表示之間的轉(zhuǎn)換方法。關(guān)于進(jìn)制轉(zhuǎn)換可以參考一些內(nèi)置函數(shù)。
hexlify 與 unhexlify
>>> import binascii >>> a = 'hello world' >>> b = binascii.b2a_hex(a) Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> b = binascii.b2a_hex(a) TypeError: 'str' does not support the buffer interface >>> a = b'hello world' >>> b = binascii.b2a_hex(a) >>> print(b) b'68656c6c6f20776f726c64' >>> b = binascii.hexlify(a) >>> print(b) b'68656c6c6f20776f726c64' >>> print(binascii.unhexlify(b)) b'hello world'上面的函數(shù) hexlify 和 b2a_hex 實際上是一個函數(shù),推薦使用 hexlify。其作用是返回的二進(jìn)制數(shù)據(jù)的十六進(jìn)制表示。每個字節(jié)的數(shù)據(jù)轉(zhuǎn)換成相應(yīng)的 2 位十六進(jìn)制表示。因此產(chǎn)生的字符串是原數(shù)據(jù)的兩倍長度。a2b_hex 和 unhexlify 則執(zhí)行反向操作。
?
相關(guān)閱讀
1. 內(nèi)置函數(shù)
轉(zhuǎn)載于:https://www.cnblogs.com/stacklike/p/8116811.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的Python的功能模块[3] - binascii - 编码转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javaweb学习总结(十)——Http
- 下一篇: 全栈Python 必备库