Python3bytes、hex、字符串之间相互转换
生活随笔
收集整理的這篇文章主要介紹了
Python3bytes、hex、字符串之间相互转换
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、字符串轉(zhuǎn)bytes
string to bytes eg: '0123456789ABCDEF0123456789ABCDEF' b'0123456789ABCDEF0123456789ABCDEF' ''' def stringTobytes(str):return bytes(str,encoding='utf8')?
2、bytes轉(zhuǎn)字符串
''' bytes to string eg: b'0123456789ABCDEF0123456789ABCDEF' '0123456789ABCDEF0123456789ABCDEF' ''' def bytesToString(bs):return bytes.decode(bs,encoding='utf8')?
3、十六進(jìn)制字符串轉(zhuǎn)bytes
''' hex string to bytes eg: '01 23 45 67 89 AB CD EF 01 23 45 67 89 AB CD EF' b'\x01#Eg\x89\xab\xcd\xef\x01#Eg\x89\xab\xcd\xef' ''' def hexStringTobytes(str):str = str.replace(" ", "")return bytes.fromhex(str)# return a2b_hex(str)?
4、bytes轉(zhuǎn)十六進(jìn)制字符串
''' bytes to hex string eg: b'\x01#Eg\x89\xab\xcd\xef\x01#Eg\x89\xab\xcd\xef' '01 23 45 67 89 AB CD EF 01 23 45 67 89 AB CD EF' ''' def bytesToHexString(bs):# hex_str = ''# for item in bs:# hex_str += str(hex(item))[2:].zfill(2).upper() + " "# return hex_strreturn ''.join(['%02X ' % b for b in bs])?
總結(jié)
以上是生活随笔為你收集整理的Python3bytes、hex、字符串之间相互转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS搭建安装SVN
- 下一篇: python netifaces模块 获