crossz

16-bit binary data interpreting difference between Java and Matlab

In java, matlab on July 22, 2009 at 11:13 am

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.
======================

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.

Simplest procedure to TRIPLE BOOT on Macbook

In howto on July 21, 2009 at 8:44 am

The must:

  • Only ONE partition for Linux; Only ONE partition for Windows. (to satisfy Windows)
  • Windows must locates at the last partition. (to satisfy Macbook)
  • Before installing, ensure the partitions are ready and good! (Disk Utility normally can not do this, or do half of this task. Because it will create several ’spare spaces’ between each partitions made by it, these partitions always make the Windows installation fail due to more than 4 primary partitions existing. But Disk Utility can create one more partition from the original one whole partition, then split it using other ways, which includes ‘diskutil’ in Mac, ‘gparted’ from Linux Livecd or even the Windows installer during Windows setup). Any partitions modification will make Windows can not boot up even synced by rEfit.
  • - If Windows has been installed by Boot Camp. Try to change boot.ini in my windows partition (disk util create a new partition between mac and windows and messed up my boot.ini, the partition it tried to boot (third) is now linux Hd instead of windows (become 4th. This might be your problem. It gave me BSoD right after splash screen)

So the procedure:

  1. Partitioning (DO NOT USE BOOT CAMP ASSISTANT)
  2. sync mbr/gpt using rEfit. (maybe can be ignored)
  3. Install Winxp on the 4th (last) partition.
  4. sync mbr/gpt using rEfit.(maybe can be ignored)
  5. Install Linux.

Java read and write 16-bit data file

In java on July 20, 2009 at 11:32 pm
  • Read 16 bit binary file:

    File file = new File(“FILENAME.bin”);
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream( bis );

    short[] myarray = new short[256000];
    int cnt;
    for (cnt = 0; cnt < 256000; cnt++) {

    myarray[cnt] = dis.readShort();

    }
    fis.close();

    Alternatively:

    RandomAccessFile dis = new RandomAccessFile(new File(“FILENAME.bin”), “r”);

    Note:
    1) bufferedInputStream can be ignored, but it will improve the performance 90% for large file operation.
    2) RandomAccessFile works like a ‘pointer’, similar to fread in Matlab, offset or skip can be assign when data retrieving while not really load all the file. This class has both read and write methods, it is a convenience class, like filereader and filewriter, while the former method is kinda low level methods.

  • Write 16 bit binary file:
    FileOutputStream fos = new FileOutputStream(new File(“OUTPUT.bin”));
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    DataOutputStream dos = new DataOutputStream(bos);

    for (cnt = 0; cnt < 256000; cnt++) {

    myarray[cnt] = dis.readShort();

    }

    bos.flush();

    fos.close();

    Alternatively:

    RandomAccessFile dos = new RandomAccessFile(new File(“OUTPUT.bin”), “rw”);

    Note: The following step is essential!!!! Otherwise, there will be some data lost.

    bos.flush();

  • Matlab:

    fid1 = fopen(‘filename.bin’, ‘r’);
    A = fread(fid1, 100, ‘uint16=>float32′);
    fclose(fid1);

    fid2 = fopen(‘output.bin’, ‘w’);
    fwrite(fid2, A, ‘uint16′);
    fclose(fid2);