Octave/matlab:要浮动的 uint8 数组

Octave/matlab: uint8 array to float

我正在从 Octave 中的套接字(使用套接字包)接收大端数据。

此数据的某些部分包含浮点(32 位)值,但这些值被编码为 uint8。

例如:(67 128 0 0)

Octave有没有办法计算对应的float/single值?

方法是使用 typecast():

octave> typecast (uint8 ([67 128 0 0]), "single")
ans =    4.6012e-41

如果您遇到字节顺序问题,请使用 swapbytes():

octave> swapbytes (typecast (uint8 ([67 128 0 0]), "single"))
ans =  256

如果您想更多地使用它,或者需要更多的灵活性,另请查看 bitpack() and bitunpack():

octave> data = uint8 ([67  128    0    0   67  127    0    0   67  126    0    0   67  125    0    0])
data =

   67  128    0    0   67  127    0    0   67  126    0    0   67  125    0    0

octave> bitpack (bitunpack (flipud (reshape (data, 4, []))), "single")
ans =

   256
   255
   254
   253