1.python2将base64数据写成图片,并将数据转为16进制字符串的方法import binascii
img = u'R0lGODlhagAeAIcAAAAAAAAARAAAiAAAzABEAABERABEiABEzACIAACIRACIiACIzADMAADMRADMiADMzADd3REREQAAVQAAmQAA3QBVAABVVQBMmQBJ3QCZAACZTACZmQCT3QDdAADdSQDdkwDungDu7iIiIgAAZgAAqgAA7gBmAABmZgBVqgBP7gCqAACqVQCqqgCe7gDuAADuTwD/VQD/qgD//zMzMwAAdwAAuwAA/wB3AAB3dwBduwBV/wC7AAC7XQC7uwCq/wD/AEQAREQAiEQAzEREAEREREREiEREzESIAESIRESIiESIzETMAETMRETMiETMzEQAAFUAAFUAVUwAmUkA3VVVAFVVVUxMmUlJ3UyZAEyZTEyZmUmT3UndAEndSUndk0nd3U/u7mYAAGYAZlUAqk8A7mZmAGZmZlVVqk9P7lWqAFWqVVWqqk+e7k/uAE/uT0/unlX/qlX//3cAAHcAd10Au1UA/3d3AHd3d11du1VV/127AF27XV27u1Wq/1X/AFX/VYgAiIgAzIhEAIhERIhEiIhEzIiIAIiIRIiIiIiIzIjMAIjMRIjMiIjMzIgAAIgARJkATJkAmZMA3ZlMAJlMTJlMmZNJ3ZmZAJmZTJmZmZOT3ZPdAJPdSZPdk5Pd3ZkAAKoAAKoAVaoAqp4A7qpVAKpVVapVqp5P7qqqAKqqVaqqqp6e7p7uAJ7uT57unp7u7qr//7sAALsAXbsAu6oA/7tdALtdXbtdu6pV/7u7ALu7Xbu7u6qq/6r/AKr/Var/qswAzMxEAMxERMxEiMxEzMyIAMyIRMyIiMyIzMzMAMzMRMzMiMzMzMwAAMwARMwAiN0Ak90A3d1JAN1JSd1Jk91J3d2TAN2TSd2Tk92T3d3dAN3dSd3dk93d3d0AAN0ASe4AT+4Anu4A7u5PAO5PT+5Pnu5P7u6eAO6eT+6enu6e7u7uAO7uT+7unu7u7u4AAP8AAP8AVf8Aqv8A//9VAP9VVf9Vqv9V//+qAP+qVf+qqv+qAP//Vf//qv///ywAAAAAagAeAAAI//8EDiRY0OBBhAkVLmTY0OFDiAVtTKQY0eJFjBkXUuTIUeNHkCEZdhRZ0uRJlA1tpGTZEuHKlS5luow5U6A7XrzcDfTosOPPnwvd7bTJUBOaPGiI4tQZ1CBQqFAN8lqVp6jRVbzy7HS3CmmebhiHWnRnoypRmTURZuWliesqTVrDXsxjI8+qfzhHusuTcy7Nf2oLwqXq9l83TVlX/T1IFW/BxN3u/uOFxi6vlxP9Yp4peLAmyToPg6XK+DCvlZXzWC14N/S/vt2yJky99fHVg1Vhc27rGC1qyxPhulMKeRVfzH1LL+ym1DBug6uObxWodTda4t1QI9eKVuDkmEq7M+0Huxh6dLdo5lrvS5Dv4r59zXr/F5d4XvGaGHLnfH5g4vFwOi6ughDLarWKIMvJqu50E6q0/jRKMCLplusqK9AKwgmso1C77b8FqbItQg3H6k6/jyaEyJ3E3isspww1vGu79tzrS8DYCGysLbzYoy+iijxrSC7H5IvqyImqu6sq5XTUEClN8oiSl9ck/MgrxaiM8sf/jpPMtOo0SUzK1biEbTGqcpLOTNwKgwuphdpabTEu3enmzpxIJEjOPAnz7yCmREPSBsdEA+nC1aj8MyQkxRprUUhTEjJSSgVSsVJMA/tz0kwXvbTTSDmNNCAAOw=='
data = base64.b64decode(img)
print type(data) #
hex_data = binascii.b2a_hex(data)
print type(hex_data) #
2.python将base64数据写成图片的方法,并将数据转为16进制字符串的方法import base64
img = 'R0lGODlhagAeAIcAAAAAAAAARAAAiAAAzABEAABERABEiABEzACIAACIRACIiACIzADMAADMRADMiADMzADd3REREQAAVQAAmQAA3QBVAABVVQBMmQBJ3QCZAACZTACZmQCT3QDdAADdSQDdkwDungDu7iIiIgAAZgAAqgAA7gBmAABmZgBVqgBP7gCqAACqVQCqqgCe7gDuAADuTwD/VQD/qgD//zMzMwAAdwAAuwAA/wB3AAB3dwBduwBV/wC7AAC7XQC7uwCq/wD/AEQAREQAiEQAzEREAEREREREiEREzESIAESIRESIiESIzETMAETMRETMiETMzEQAAFUAAFUAVUwAmU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v4r59zXr/F5d4XvGaGHLnfH5g4vFwOi6ughDLarWKIMvJqu50E6q0/jRKMCLplusqK9AKwgmso1C77b8FqbItQg3H6k6/jyaEyJ3E3isspww1vGu79tzrS8DYCGysLbzYoy+iijxrSC7H5IvqyImqu6sq5XTUEClN8oiSl9ck/MgrxaiM8sf/jpPMtOo0SUzK1biEbTGqcpLOTNwKgwuphdpabTEu3enmzpxIJEjOPAnz7yCmREPSBsdEA+nC1aj8MyQkxRprUUhTEjJSSgVSsVJMA/tz0kwXvbTTSDmNNCAAOw=='
data = base64.b64decode(img)
print(type(data)) #
hex_imgdata = ''.join(["%02X" % x for x in bytes_imgdata]).strip()
print(type(hex_imgdata)) #
with open('ddd.png', 'wb') as f: f.write(data)
f.write(data)
3.python 字符串与16进制互转1 ByteToHex的转换
def ByteToHex( bins ):
return ''.join( [ "%02X" % x for x in bins ] ).strip()
返回数据16进制字符串 '91f8148cfbd5faa3d98b'
2.bytes类型转为16进制bytes类型
import binascii
sign 为bytes类型字符串 如 b'\x91\xf8\x14\x8c\xfb\xd5|'
print(type(sign)) >>
ret = binascii.b2a_hex(sign) ret为16进制bytes
print(ret) 》》 如 b'91f8148cfbd5faa3d98b'
3 HexToByte的转换
def HexToByte( hexStr ):
return bytes.fromhex(hexStr)
>>> binascii.b2a_hex(u"你好啊".encode("utf8"))
'e4bda0e5a5bde5958a'
>>>
>>> binascii.b2a_hex(u"你好啊".encode("gbk"))
'c4e3bac3b0a1'
>>> binascii.a2b_hex("e4bda0e5a5bde5958a")
'\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x95\x8a'
>>> binascii.a2b_hex("e4bda0e5a5bde5958a").decode("utf8")
u'\u4f60\u597d\u554a'
>>> print binascii.a2b_hex("e4bda0e5a5bde5958a").decode("utf8")
你好啊
>>> binascii.b2a_hex(u"你好啊121A号".encode("gbk"))
'c4e3bac3b0a131323141bac5'
>>> '48656c6c6f'.decode('hex')
'Hello'
>>> 'c4e3bac3b0a131323141bac5'.decode('hex')
'\xc4\xe3\xba\xc3\xb0\xa1121A\xba\xc5'
>>> print 'c4e3bac3b0a131323141bac5'.decode('hex')
你好啊121A号
>>> u"你好啊121A号".encode("gbk").encode('hex')
'c4e3bac3b0a131323141bac5'
>>>
3.Python 3中bytes/string的区别
python 3中最重要的新特性可能就是将文本(text)和二进制数据做了更清晰的区分。文本总是用unicode进行编码,以str类型表示;而二进制数据以bytes类型表示。
在python3中,不能以任何隐式方式将str和bytes类型二者混合使用。不可以将str和bytes类型进行拼接,不能在str中搜索bytes数据(反之亦然),也不能将str作为参数传入需要bytes类型参数的函数(反之亦然)。
字符串和字节符之间划分界线是必然的。下面这个图解要牢记于心:
strings可以被编码(encode)成字bytes,bytes也可以解码(decode)成strings:>>> '€20'.encode('utf-8')
b'\xe2\x82\xac20'
>>> b'\xe2\x82\xac20'.decode('utf-8')
'€20'
可以这样理解:
string是文本(text)的抽象表示。字符串(string)由字符组成,字符也是抽象的实体且与任何二进制表示无关。
当操纵字符串的时候,很多细节是不用了解的。我们可以分割、切片和拼接字符串,在字符串内部进行搜索。但并不在乎内部是如何表示的,也不用在意底层一个字符要花费多少byte。
只有在需要将string编码(encode)成byte的时候,比如:通过网络传输数据;或者需要将byte解码(decode)成string的时候,我们才会关注string和byte的区别。
传入encode和decode的参数是编码方式。编码是一种用二进制数据表示抽象字符的方式。目前有很多种编码。上面给出的UTF-8是其中一种,下面是另一种:>>> '€20'.encode('iso-8859-15')
b'\xa420'
>>> b'\xa420'.decode('iso-8859-15')
'€20'
编码是这个转换过程中至关重要的一部分。若不编码,bytes对象b'\xa420'只是一堆比特位而已。编码赋予其含义。采用不同的编码,这堆比特位的含义就会大不同:>>> b'\xa420'.decode('windows-1255')
'₪20'
4.python2和python3中str,bytes区别
python2中,有basestring、str、bytes、unicode四种类型
其中str == bytes , basestring = (str,unicode)>>> isinstance('s',str)
True
>>> isinstance('s',bytes)
True
>>> isinstance('s',unicode)
False
>>> isinstance('s'.decode(),unicode)
True
>>> isinstance('s'.decode(),basestring)
True
>>> isinstance('s',basestring)
True
python3中,有str和bytes类型>>> isinstance('s',str)
True
>>> isinstance('s',bytes)
False
>>> isinstance('s',unicode)
Traceback (most recent call last):
File "", line 1, in
NameError: name 'unicode' is not defined
>>> isinstance(b's',bytes)
True
>>> isinstance('s',basestring)
Traceback (most recent call last):
File "", line 1, in
NameError: name 'basestring' is not defined
参考:
https://www.cnblogs.com/abclife/p/7445222.html
https://blog.csdn.net/weixin_37989267/article/details/79432242
https://www.cnblogs.com/leomei91/p/7681416.html