Search code examples
javavb5

Read VB 5.0 binary files with Java


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 );
    }

Solution

  • 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);