最近在搞阿里的物联网,其中使用到二进制数据透传,后台使用python,所以需要研究一下,二进制数据的传输问题。
Python版本:3.6
设备端发送二进制数据:
7F 00 41 41 16 03 1E 1F 03 02 1F 1E 02 9C 9C EA EA 16 57 58 59 5A 5B 5C 5D 5E 5F 60 61 57 58 59 5A 5B 5C 5D 5E 5F 60 61 50 51 52 53 54 55 56 57 58 59 5A 50 51 52 53 54 55 56 57 58 59 5A 7E 02 7E 01 3E 3F 7F
物联网平台接受数据并进行base64编码,数据:
fwBBQRYDHh8DAh8eApyc6uoWV1hZWltcXV5fYGFXWFlaW1xdXl9gYVBRUlNUVVZXWFlaUFFSU1RVVldYWVp+An4BPj9/
代码:
import base64
src ="7F 00 41 41 16 03 1E 1F 03 02 1F 1E 02 9C 9C EA EA 16 57 58 59 5A 5B 5C 5D 5E 5F 60 61 57 58 59 5A 5B 5C 5D 5E 5F 60 61 50 51 52 53 54 55 56 57 58 59 5A 50 51 52 53 54 55 56 57 58 59 5A 7E 02 7E 01 3E 3F 7F"
src = src.replace(" ","")
res = base64.b64encode(bytearray.fromhex(src))
print(res)
print(res.decode())
temp ="fwBBQRYDHh8DAh8eApyc6uoWV1hZWltcXV5fYGFXWFlaW1xdXl9gYVBRUlNUVVZXWFlaUFFSU1RVVldYWVp+An4BPj9/"
vv = base64.b64decode(temp.encode())
print("++++++++++++++++++++++++++++++++++++++++++++")
print(vv)
print(len(vv))
for iin range(len(vv)):
print('{:08b}'.format(vv[i]))
print("++++++++++++++++++++++++++++++++++++++++++++")