File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# Integer.toOctalString

ishmayel vemuru
Ranch Hand

Joined: Jun 13, 2007
Posts: 46
Hi to every one..
I have some doubt in the following examle any one help me to understand this...

class Octal
{
public static void main(String[] args)
{
int a=0254;

String oct=Integer.toOctalString(254);
String hex=Integer.toHexString(254);
System.out.println("a---------"+a); // line 1
System.out.println("oct---------"+oct); //line 2
System.out.println("hex---------"+hex); //line 3
}
}

I understood the out put produced at line 1-----172
I am unable to undertand the output produced at line 2 (out put: 376) and at line 3 (out put: fe ).
If any one have idea please hlep me to understand this...
Ishmayel
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
Do you know what octal and hexadecimal means?

Octal has a base of 8 instead of 10.

So 215 octal is 2x(8power2) + 1x(8power1) + 5x(8power0) = 2x64 + 1x8 + 5x1

215 normally is 2x(10power2) + 1x(10power1) + 5x(10power0) = 2x100 + 1x10 + 5x1

Hexadecimal has a base of 16. Can you figure out what 215 is hexadecimal?

Java hobbyist.
Ranch Hand

Joined: Jun 16, 2008
Posts: 75
The first one 0254 is an octal representation and hence it is converted to decimal
An Octal Representation is possible by prefixing a zero before the number.

We get ,
a = 172
as an octal gets converted to decimal

In the line 2

Decimal gets converted to octal

In the line 3

Decimal gets converted to hexa decimal

Explanation for line 2 :
base 10 is decimal representation
254 (base 10) = x (base 8)

Have to find x

which is 8 L 254
8 L 31 - 6
8 L 3 - 7
0 - 3

When we look after the reminder , we get 376 which is the output

Similarily for line 3,

254 (base 10) = x (base 16)

Have to find x

which is 16 L 254
16 L 15 - 14
0 - 15

0 - 9 | 0 - 9
10 | a
11 | b
12 | c
13 | d
14 | e
15 | f
16 | 10

Hence the reminder s are 15 and 14
ie. f and e
Hence, fe.

http://chortle.ccsu.edu/AssemblyTutorial/Chapter-07/ass07_1.html

Regards,<br />Pradeep Balasubramanian<br /> <br />You have to grow from the inside out. None can teach you, none can make you spiritual. There is no other teacher but your own soul. <br /> <br />- Swami Vivekananda
ishmayel vemuru
Ranch Hand

Joined: Jun 13, 2007
Posts: 46
Hi..
Ronald Schild and Prathip Bala...

Ishmayel.

I agree. Here's the link: http://aspose.com/file-tools

subject: Integer.toOctalString