Wednesday 22 July 2009

16-bit binary data interpreting difference between Java and Matlab

The 16-bit data will be interpreted as different values in Java and Matlab. WHY??

Here are some experiments: save number 15 to 8-bit and 16-bit binary files, and another 259 in 16-bit binary files (refer to : this post).
======================

In Matlab (fread(fid1, 'uint8');): 15

16 bit bin file containing 15:
In WinHex: 0F 00
In java (DataInputStream.readShort();): [3840, 0]
=> 3840/256=15;
=> so, it's a kind of [15 00]
In Matlab (fread(fid1, 'uint16');): 15

16 bit bin file containing 259:
In WinHex: 03 01
In java (DataInputStream.readShort();): [769, 0] //Note, for every byte, java can

contain a value larger than 256, while other programming language will increment.
=> 769%256 = 1; 769/256=3.0039;
=> so, it's a kind of [03 01]
In Matlab (fread(fid1, 'uint16');): 259

Therefore:
========================
Therefore, a integer value in the range of 16-bit, for example 41837. In Matlab,

it will be intepreted as 41837, mod(41837,256)=109, 41837/256=163.4. So 41837 can

be displayed as [163 109] = 163*256+109; while java intepret [163 109] as

163+109*256 = 28067;


Conclusion:
========================
This seems wrong inside Java, but Java know how to process such value and export

it correctly to another bin file, which one can be understood by Matlab, WinHex or

other programming languages or editor.

No comments:

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter