1

I'm trying to convert an base 10 number to a base 2 and back to base 10. It works only for positive argument_decimal

argument_binary = Integer.toBinaryString(argument_decimal);
back_converted_argument_decimal = Integer.valueOf(argument_binary, 2);

For argument_decimal beeing negative, I get "java.lang.NumberFormatException: For input string: "11111111111111111111111111111111""

EDIT: here is what I do:

latitude_binary = Integer.toBinaryString((int)(latitude_decimal * 1000000));   
back_converted_latitude_decimal =  Long.parseLong(latitude_binary, 2) / 1000000.0;

which gives me bad results like -1.1 being forth and back converted to 4293.867296

6
  • You mean base 2 and base 10, correct? Commented Dec 23, 2012 at 15:09
  • correct. sorry for confusion... Commented Dec 23, 2012 at 15:10
  • Your input string is 33 characters, so this is not surprising; Java integers only cover 32 bits. So, is there really a problem? Commented Dec 23, 2012 at 15:11
  • my argument_decimal was "-1", I did not set argument_binary manually to 33 ones, if that's what you suggest... Commented Dec 23, 2012 at 15:11
  • 1
    The context is far more complex to describe it here in my broken english ;)... I just need it... Commented Dec 23, 2012 at 15:19

2 Answers 2

8

Try to go via a long:

String binary = Integer.toBinaryString(-1);
long l = Long.parseLong(binary, 2);
int i = (int) l;

Tested, and working.

Why this works is because -1 is represented as a sequence of 32 bits 1 in system memory. When using the toBinaryString method, it creates a string using that exact representation. But, 32 bits of one is in fact equal to 2^32 - 1. That is too large for an int (4 bytes), because an int goes from [-2^31, 2^31-1]. This is because the most left bit is representing the sign. So to fix that overflow, first interpret that sequence of 1 and 0 characters as a Long. A long will do because the maximum value for a long is 2^63-1. Then convert the long to an int. This is done by simply taking the lower 32 bits.


The bug in your code is that you didn't cast the Long.parseLong to an int. So this should work:

lat_bin = Integer.toBinaryString((int)(lat_dec * 1000000));   
lat_dec_conv =  ((int) Long.parseLong(lat_bin, 2)) / 1000000.0;
Sign up to request clarification or add additional context in comments.

1 Comment

And for numbers larger than 64-bits, BigInteger.
0
 public static void convertStringToDecimal(String binary) {
    int decimal = 0;

    int power = 0;

    if (binary.charAt(0) == '1' && binary.length() == 32) {

        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < binary.length(); i++) {

            builder.append((binary.charAt(i) == '1' ? '0' : '1'));

        }

        while (binary.length() > 0) {

            int temp = Integer
                    .parseInt(builder.charAt((binary.length()) - 1)+"");
            decimal += temp * Math.pow(2, power++);
            binary = binary.substring(0, binary.length() - 1);

        }

        System.out.println((decimal + 1) * (-1));

    } else {

        while (binary.length() > 0) {
            int temp = Integer
                    .parseInt(binary.charAt((binary.length()) - 1) + "");
            decimal += temp * Math.pow(2, power++);
            binary = binary.substring(0, binary.length() - 1);
        }

        System.out.println(decimal);

    }
}

1 Comment

@MarlonAbeykoon The first if statement checks if the number passed has its' leftmost bit equal to '1' and it case it is the number is negative. Next step is to inverse bits and calculate the decimal representation. If you are confused by the calculation you must look into "two's complement" . Hope it helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.