11-23-2020 06:55 AM - edited 11-23-2020 06:57 AM
Hello.
I am trying to read data of data type I16 by executing the code below in python3.
=========================================================
Python3 Code
=========================================================
raw_byte = b'\x01\x80'
t1 = np.frombuffer(raw_byte, dtype='<f2')[0]
print(t1) # result => -6e-08
The correct result should be 0.099999999999999
=========================================================
I16 DATA
=========================================================
01 80 7A 8D F3 9A 6C A8 E5 B5 5E C3 D7 D0 50 DE C9 EB 42 F9 BC 06 35 14 AE 21 27 2F A0 3C 19 4A 92 57 0B 65 84 72 FD 7F
=========================================================
However, the result is different from the channel content data value of the Diadem program.
Even looking at the help, I can't figure out what to do.
I am working on the 3rd day, but I can't solve it.
Please help me. ㅠㅠ
Thanks.
11-25-2020 03:29 AM - edited 11-25-2020 03:37 AM
I am not familiar with numpy, but if you want to read I16 I assume your format string is wrong.
I16 must be numeric between [-32767, +32767]
Using struct (part of native python) you need to use "h" as format for I16.
import struct
raw_byte = b'\x01\x80'
i16_bigEndian = struct.unpack('>h', raw_byte)[0]
i16_littleEndian = struct.unpack('<h', raw_byte)[0]
print(i16_bigEndian)
print(i16_littleEndian)
returns
384
-32767
depending on the endianess.
Reading the complete dataset would look like
raw_byte = b'\x01\x80\x7A\x8D\xF3\x9A\x6C\xA8\xE5\xB5\x5E\xC3\xD7\xD0\x50\xDE\xC9\xEB\x42\xF9\xBC\x06\x35\x14\xAE\x21\x27\x2F\xA0\x3C\x19\x4A\x92\x57\x0B\x65\x84\x72\xFD\x7F'
i16_bigEndian = struct.unpack('>20h', raw_byte)
i16_littleEndian = struct.unpack('<20h', raw_byte)
print(i16_bigEndian)
print(i16_littleEndian)
returning
(384, 31373, -3174, 27816, -6731, 24259, -10288, 20702, -13845, 17145, -17402, 13588, -20959, 10031, -24516, 6474, -28073, 2917, -31630, -641)
(-32767, -29318, -25869, -22420, -18971, -15522, -12073, -8624, -5175, -1726, 1724, 5173, 8622, 12071, 15520, 18969, 22418, 25867, 29316, 32765)