This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
The Integer class has a variety of methods you can choose between. Assuming you're starting with a String which represents a number in decimal, and you want a String representing it in octal: <code><pre> int value = Integer.parseInt(decimalString); String octalString = Integer.toOctalString(value);</pre></code>
"I'm not back." - Bill Harding, Twister
Joined: Nov 22, 2008
I have to convert a character value to a hex or Octal. Example : char c = new char; Now all the elements would be initialized to " ". How do we convert an array element (say c) value to a hex or octal. Should we use the wrapper class Character for this? Thanks..
Joined: Jan 30, 2000
Of the wrapper classes, only Integer and Long have the full set of conversion methods you need. But since char is an integral type, so you pass it to a method which accepts an integer argument - it's a widening conversion, which means no cast is necessary. (i.e. since the compiler knows that any char can be represented by an int, it will do the conversion for you without you needing to coax it.) So: <code><pre> String octalString = Integer.toOctalString(c);</pre></code>