1. 程式人生 > >python2/3中 將base64資料寫成圖片,並將圖片資料轉為16進位制資料的方法、bytes/string的區別

python2/3中 將base64資料寫成圖片,並將圖片資料轉為16進位制資料的方法、bytes/string的區別

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+q////AP//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) # <type 'str'> hex_data = binascii.b2a_hex(data) print type(hex_data) # <type 'str'>
with open('ddd.png', 'wb') as f:
    f.write(data)

2.python將base64資料寫成圖片的方法,並將資料轉為16進位制字串的方法

import base64
img = 'R0lGODlhagAeAIcAAAAAAAAARAAAiAAAzABEAABERABEiABEzACIAACIRACIiACIzADMAADMRADMiADMzADd3REREQAAVQAAmQAA3QBVAABVVQBMmQBJ3QCZAACZTACZmQCT3QDdAADdSQDdkwDungDu7iIiIgAAZgAAqgAA7gBmAABmZgBVqg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q9l83TVlX/T1IFW/BxN3u/uOFxi6vlxP9Yp4peLAmyToPg6XK+DCvlZXzWC14N/S/vt2yJky99fHVg1Vhc27rGC1qyxPhulMKeRVfzH1LL+ym1DBug6uObxWodTda4t1QI9eKVuDkmEq7M+0Huxh6dLdo5lrvS5Dv4r59zXr/F5d4XvGaGHLnfH5g4vFwOi6ughDLarWKIMvJqu50E6q0/jRKMCLplusqK9AKwgmso1C77b8FqbItQg3H6k6/jyaEyJ3E3isspww1vGu79tzrS8DYCGysLbzYoy+iijxrSC7H5IvqyImqu6sq5XTUEClN8oiSl9ck/MgrxaiM8sf/jpPMtOo0SUzK1biEbTGqcpLOTNwKgwuphdpabTEu3enmzpxIJEjOPAnz7yCmREPSBsdEA+nC1aj8MyQkxRprUUhTEjJSSgVSsVJMA/tz0kwXvbTTSDmNNCAAOw==
' data = base64.b64decode(img) print(type(data)) # <class 'bytes'> hex_imgdata = ''.join(["%02X" % x for x in bytes_imgdata]).strip() print(type(hex_imgdata)) # <class 'str'> 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)) >> <class 'bytes'> 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:

1 2 3 4 >>> '€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是其中一種,下面是另一種:

1 2 3 4 >>> '€20'.encode('iso-8859-15') b'\xa420' >>> b'\xa420'.decode('iso-8859-15') '€20'

編碼是這個轉換過程中至關重要的一部分。若不編碼,bytes物件b'\xa420'只是一堆位元位而已。編碼賦予其含義。採用不同的編碼,這堆位元位的含義就會大不同:

1 2 >>> 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 "<stdin>", line 1, in <module>
NameError: name 'unicode' is not defined
>>> isinstance(b's',bytes)
True
>>> isinstance('s',basestring)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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