File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to convert a number to Hexadecimal or Octal format.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to convert a number to Hexadecimal or Octal format.." Watch "How to convert a number to Hexadecimal or Octal format.." New topic
Author

How to convert a number to Hexadecimal or Octal format..

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
Can anyone please tell me, what method can we use to convert a decimal number to Hexadecimal/Octal and vice-versa?
Thanks..
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I have to convert a character value to a hex or Octal.
Example : char[] c = new char[10]; Now all the elements would be initialized to " ". How do we convert an array element (say c[5]) value to a hex or octal. Should we use the wrapper class Character for this?
Thanks..

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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[5]);</pre></code>

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks Jim.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to convert a number to Hexadecimal or Octal format..