kei23th
kei23th

Reputation: 283

How do I convert a signed decimal value to a 32 bit little-endian binary string?

I need to convert a signed decimal number into a 32 bit little-endian binary value. Does anyone by any chance know of a built-in Java class or function that can do this? Or have built one to do this?

The data is a longtitude/latitude value like -78.3829. Thanks for any help.

Upvotes: 0

Views: 3058

Answers (2)

jrad
jrad

Reputation: 3190

If it helps at all, here's a class that I made that converts longs to binary Strings and binary Strings to longs:

public class toBinary {

    public static void main(String[] args) {
        System.out.println(decimalToBinary(16317));
        System.out.println(binaryToDecimal("11111111111111111111111111111111111100101001"));
    }

    public static long binaryToDecimal(String bin) {
        long result = 0;
        int len = bin.length();
        for(int i = 0; i < len; i++) {
            result += Integer.parseInt(bin.charAt(i) +  "") * Math.pow(2, len - i - 1);
        }
        return result;
    }

    public static String decimalToBinary(long num) {
        String result = "";
        while(true) {
            result += num % 2;
            if(num < 2)
                break;
            num = num / 2;
        }
        for(int i = result.length(); i < 32; i++)
            result += "0";
        result = reverse(result);
        result = toLittleEndian(result);
        return result;
    }

    public static String toLittleEndian(String str) {
        String result = "";
        result += str.substring(24);
        result += str.substring(16, 24);
        result += str.substring(8, 16);
        result += str.substring(0, 8);
        return result;
    }

    public static String reverse(String str) {
        String result = "";
        for(int i = str.length() - 1; i >= 0; i--)
            result += str.charAt(i);
        return result;
    }

}

It doesn't take decimal values, but it could probably give you a bit of guidance.

Upvotes: 2

Durandal
Durandal

Reputation: 20059

The conversion is trivial once you know what the endianess means on binary level. The question is more what do you really want to do with it?

public static int flipEndianess(int i) {
    return (i >>> 24)          | // shift byte 3 to byte 0
           ((i >> 8) & 0xFF00) | // shift byte 2 to byte 1
           (i << 24)           | // shift byte 0 to byte 3
           ((i & 0xFF00) << 8);  // shift byte 1 to byte 2
}

This little method will swap around the bytes in an int to switch between little/big endian order (the conversion is symetric). Now you have a little endian int. But what would you do with that in Java?

More likely you need to write the data to a stream or something, then its only a question in which order you write the bytes out:

// write int to stream so bytes are little endian in the stream
// OutputStream out = ... 
out.write(i);
out.write(i >> 8);
out.write(i >> 16);
out.write(i >> 24);

(For big endian you would just order the lines from bottom to top...)

Upvotes: 0

Related Questions