3

I have a binary file that is created from a program made in Visual Basic 5.0. The file just contains a bunch of Long values from the Visual Basic world. I've understood that Long in Visual Basic 5.0 is 4 bytes in size, but I do not know the byte order.

I tried parsing the file with DataInputStream using various "read"-methods, but I seem to get "wrong" (i.e. negative) values.

How can I read this and interpret it correctly with Java? What is the byte order for a Long in Visual Basic 5.0?

Below is some kind of code I'm trying to work with; I'm trying to read 2 Long s and print the out on the screen, and then read 2 more etc.

    try {
        File dbFile = new File(dbFolder + fileINA);
        FileInputStream fINA = new FileInputStream(dbFile);
        dINA = new DataInputStream(fINA);
        long counter = 0;

        while (true) {
            Integer firstAddress = dINA.readInt();
            Integer lastAddress = dINA.readInt();

            System.out.println(counter++ + ": " + firstAddress + " " + lastAddress);
        }
    }
    catch(IOException e) {
        System.out.println ( "IO Exception =: " + e );
    }
Franz
  • 1,993
  • 13
  • 20
  • 4 bytes means an int in Java, and all primitives in Java are signed, so it can still be correct even though you see negative values. – Kaj Jun 20 '11 at 11:23
  • If you posted the actual code you used and what incorrect results you got, it would be easier to help you. – Gabe Jun 20 '11 at 11:30
  • Gabe: Posted the code I'm trying to work with. – Franz Jun 20 '11 at 11:42
  • OK, good. Now if you know what the results are supposed to be, you can post the sample input along with the incorrect output and what you want the output to be, and we can tell you exactly what's wrong. – Gabe Jun 20 '11 at 11:48

1 Answers1

5

Since VB runs on x86 CPUs, its data types are little-endian. Also, note that a Long in VB is the same size as an int in Java.

I would try something like this:

int vbLong = ins.readUnsignedByte() +
             (ins.readUnsignedByte() << 8) +
             (ins.readUnsignedByte() << 16) +
             (ins.readUnsignedByte() << 24);
Gabe
  • 84,912
  • 12
  • 139
  • 238