1 2 Previous Next 28 Replies Latest reply on Mar 20, 2007 3:37 PM by JosAH

    Conversion int <-> binary

    807606
      Hello everybody,

      i want to change an integer signed value (on 32 bits) in binary format and do the reverse operation.

      So i wrote :
      String binIntStr = Integer.toBinaryString(-1610612462);
      System.out.println("Binary : "+binIntStr);
      System.out.println("Int : "+Integer.parseInt(binIntStr, 2));
      The conversion of the int to binary runs correctly, but the reverse operation (binary string to int) throws an NumberFormatException.
      I don't understand why the operation is well done in one way and is not allowed in the other.

      The integer value is in good format (-2147483648 < -1610612462 < 2147483647) so why can't I change the binary string returned by the method toBinaryString in an integer ?
        • 1. Re: Conversion int <-> binary
          DrClap
          Some useful things you could do:

          1. Look at that string to see what it contains.

          2. Try other values to see what else doesn't work.
          • 2. Re: Conversion int <-> binary
            791266
            It's rather strange. The resulting string contains 32 digits (ones and zeros) and everything looks normal.

            Kaj
            • 3. Re: Conversion int <-> binary
              807606
              because i think "10100000000000000000000100010010" is too long for integer...
              • 4. Re: Conversion int <-> binary
                791266
                because i think "10100000000000000000000100010010" is
                too long for integer...
                No 32 bits is ok.
                • 5. Re: Conversion int <-> binary
                  807606
                  you can convert each digit manually...

                  like :

                  int total = 0;
                  for(int i=0; i<binIntStr.length() ; i++)
                  {
                  String str = "" + binIntStr.charAt(i);
                  total = total + pow(Integer.parseInt(str),i);
                  }

                  something like that...
                  • 6. Re: Conversion int <-> binary
                    807606
                    correction...

                    int total = 0;
                    for(int i=0; i<binIntStr.length() ; i++)
                    {
                    String str = "" + binIntStr.charAt(i);
                    total = total + (pow(2,i) * Integer.parseInt(str));
                    }
                    • 7. Re: Conversion int <-> binary
                      807606
                      oppss....one more correction....

                      you need to modify the code so that it considers the signed bit...
                      • 8. Re: Conversion int <-> binary
                        796440
                        because i think "10100000000000000000000100010010"
                        is
                        too long for integer...
                        No 32 bits is ok.
                        Unfortunately, no. -2 is 1111...0. But you can't parse that back to an int. You have to do "-10".
                        • 9. Re: Conversion int <-> binary
                          807606
                          Thanks for you answers.

                          All is correct that's because i posted here. :)

                          Like i said -2147483648 < -1610612462 < 2147483647, and the string in binary format returned is good (its length equals to 32 bits).

                          I'd like that someone of Sun explains to me if it's normal or not.
                          If it's normal i'll do it manually, but if there is a way to do it with Sun API i prefer to use it.

                          Regards.
                          • 10. Re: Conversion int <-> binary
                            791266
                            because i think
                            "10100000000000000000000100010010"
                            is
                            too long for integer...
                            No 32 bits is ok.
                            Unfortunately, no. -2 is 1111...0. But you can't
                            parse that back to an int. You have to do "-10".
                            One would expect it to be able to parse both formats. I still find it strange :)
                            • 11. Re: Conversion int <-> binary
                              JosAH
                              You could do:
                              int i= (int)Long.parseLong(s, 2);
                              Clumsy indeed ...

                              kind regards,

                              Jos
                              • 12. Re: Conversion int <-> binary
                                796440
                                I'd like that someone of Sun explains to me if it's
                                normal or not.
                                Sun employees don't generally post here. Most of us who answer questions here do not work for Sun. You probably won't get an answer from Sun.

                                As for whether it's "normal," well, it's just behaving as the docs say. If your radix is 2, it's NOT twos-complement.
                                • 13. Re: Conversion int <-> binary
                                  807606
                                  Integer.toBinaryString() returns a string representation of the integer argument as an unsigned integer in base 2

                                  from the API doc
                                  toBinaryString
                                  public static String toBinaryString(int i)Returns a string representation of the integer argument as an unsigned integer in base 2.
                                  The unsigned integer value is the argument plus 232 if the argument is negative; otherwise it is equal to the argument.
                                  This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s. If the unsigned magnitude is zero, it is represented by a single zero character '0' ('\u0030');
                                  otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The characters '0' ('\u0030') and '1' ('\u0031') are used as binary digits.


                                  Parameters:
                                  i - an integer to be converted to a string.
                                  Returns:
                                  the string representation of the unsigned integer value represented by the argument in binary (base 2).
                                  so you will have to figure out out to deal with signed values yourself

                                  Message was edited by:
                                  SomeoneElse
                                  • 14. Re: Conversion int <-> binary
                                    807606
                                    For big integers, you should use long, if you are sure that number is representable by a long or java.lang.BigInteger:
                                    http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigInteger.html#BigInteger(java.lang.String,%20int)
                                    This class allow introduce number in any base.
                                    1 2 Previous Next