对于 ascii 值,tobytes 产生预期的字节串:
In [272]: np.array(['1','a','A','_'],'S1')
Out[272]: array([b'1', b'a', b'A', b'_'], dtype='|S1')
In [273]: _.tobytes()
Out[273]: b'1aA_'
或者对于一些小数字:
In [279]: np.arange(9).astype('uint8').tobytes()
Out[279]: b'\x00\x01\x02\x03\x04\x05\x06\x07\x08'
查看您的示例字符串:
In [303]: astr = b'\x1d\x00\xd8\xff=\x00\xa6\xff\x9b\x00\xc0\xfe\x03\n\x9b\x13\x
...: 04\x15k\x17'
In [304]: len(astr)
Out[304]: 20
它可以转换回数组,但我们必须指定dtype:
In [305]: np.frombuffer(astr,'S1')
Out[305]:
array([b'\x1d', b'', b'\xd8', b'\xff', b'=', b'', b'\xa6', b'\xff',
b'\x9b', b'', b'\xc0', b'\xfe', b'\x03', b'\n', b'\x9b', b'\x13',
b'\x04', b'\x15', b'k', b'\x17'], dtype='|S1')
In [306]: np.frombuffer(astr,'uint8')
Out[306]:
array([ 29, 0, 216, 255, 61, 0, 166, 255, 155, 0, 192, 254, 3,
10, 155, 19, 4, 21, 107, 23], dtype=uint8)
In [307]: np.frombuffer(astr,'uint17')
Traceback (most recent call last):
File "<ipython-input-307-45de956fd32b>", line 1, in <module>
np.frombuffer(astr,'uint17')
TypeError: data type 'uint17' not understood
In [308]: np.frombuffer(astr,'uint16')
Out[308]:
array([ 29, 65496, 61, 65446, 155, 65216, 2563, 5019, 5380,
5995], dtype=uint16)
===
'=' 本身就是一个字节,而不是 `\xff' 的一部分:
In [338]: astr[:6]
Out[338]: b'\x1d\x00\xd8\xff=\x00'
In [339]: list(astr[:6])
Out[339]: [29, 0, 216, 255, 61, 0]
In [340]: ord('=')
Out[340]: 61