Java Reference
In-Depth Information
System . out . println ( i + " formatted in base " + radix + " is "
+ Integer . toString ( i , radix ));
}
This program prints the binary string as an integer in various bases, and the integer 42 in
those same number bases:
$ java numbers.IntegerBinOctHexEtc
101010 in base 2 is 42; 42 formatted in base 2 is 101010
101010 in base 8 is 33288; 42 formatted in base 8 is 52
101010 in base 10 is 101010; 42 formatted in base 10 is 42
101010 in base 16 is 1052688; 42 formatted in base 16 is 2a
101010 in base 36 is 60512868; 42 formatted in base 36 is 16
$
Discussion
There are also specialized versions of toString(int) that don't require you to specify the
radix; for example, toBinaryString() to convert an integer to binary, toHexString() for
hexadecimal, and so on. The javadoc page for the Integer class is your friend here.
Going the other way, the Integer class includes toBinaryString() , toOctalString() ,
and toHexString() .
The String class itself includes a series of static methods— valueOf(int) ,
valueOf(double) , and so on—that also provide default formatting. That is, they return the
given numeric value formatted as a string.
Operating on a Series of Integers
Problem
You need to work on a range of integers.
Solution
For a contiguous set, use a for loop.
Search WWH ::




Custom Search