Peter Ream Peter Ream - 6 months ago 18
Vb.net Question

Visual Basic single data type in java

I wrote a VB program years ago. In an effort to learn a Java, I have rewritten it in Java. I have archival data that i would like to convert, so I can read it in my Java program.

I have been researching datatype representations to figure out some conversions, but have run into a brick wall mainly due to my newness to Java and time away from VB.

I have a field in VB that is single and is 69.7 (represented internally as x'66668A42'. For the life of me, I cannot figure out this internal representation. However, how do I convert this to something that Java can understand (double)?

Answer

0x66668A42 is the little endian representation of the float value 69.2

Its presumably little endian because thats the native x86 format. In contrast java defines everything as big endian, even if the underlying platform is not.

So to convert your VB single to a java float you have to shuffle the bytes into reverse order, then convert the int to float:

public class L2BFloat {

    public static void main(String[] argv) {
        int x = 0x66668A42;
        System.out.println(little2Big2Float(x));
    }

    public static float little2Big2Float(int little) {
        // int endian conversion
        int big = (little & 0x000000FF) << 24
                | (little & 0x0000FF00) << 8
                | (little & 0x00FF0000) >>> 8
                | (little & 0xFF000000) >>> 24;
        // convert to float
        return Float.intBitsToFloat(big);
    }

}
Comments