why does numpytobyte return not only hex digits but also additional characters?

For ascii values, tobytes produces the expected bytestring:

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_'

Or for some small numbers:

In [279]: np.arange(9).astype('uint8').tobytes()
Out[279]: b'\x00\x01\x02\x03\x04\x05\x06\x07\x08'

Looking at your sample string:

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

It can be converted back to array, but we have to specify the 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)

===

That ‘=’ is a byte itself, not part of `\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

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top