6 Years
Discussion Span
Last Post by JamesCherrill

The Integer class has methods for converting ints to Strings in binary, octal, hex, or any arbitrary base. Ditto for converting any of those formats back to int. Check out the API docs for details.


Are you talking about Strings in those formats? Like "111" as binary would have an int value of 7

I don't think there is a method to convert a String in one format to a String in another format. You will probably have to combine some methods to do that.


Yes. The methods in Integer allow you to parse a value in any base, returning an int, then othernmethods let you convert that int to a string in any other base.


The basic things is to keep in correct place input/output value as string and the numerical value as number, which is one suitable format suitable for calculations.

So what really happens is that your have two processes
'some numbers' -> value('some numbers')

value -> string representation of value in any desired base

Also you do not find function to directly translate string to input base7 number and output it in base 3, but you can read the value of base7 number and you can output the value of your variable in base7. It is two step process.

If you want to use base -2 probably you are out of luck in Java (which I know only superficially) as you are in Python (which I consider myself knowing reasonably well), and must roll out your own function.


I'm sorry but I'm having trouble understanding what you are saying.

I want to convert a binary to decimal. for example if the user type in 1101, I want to convert it to 13.


What data type are each of those? Is the binary number a String? Is the 13 an int?
The Integer class has a method to do that.

Otherwise please explain.


Use Integer.valueOf(String s, int radix) to parse your input string with whatever base you want (radix = 2 for binary, 16 for hex etc). That gives you an int. Then use Integer.toString(int i, int radix) to covert that int to any other base. eg

      int i = Integer.valueOf("1101", 2); // parse 1101 as binary
      System.out.println( Integer.toString(i, 8)); // print it out in octal
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.