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 Win a copy of Java Interview Guide this week in the Jobs Discussion forum!

# integral literals ?

Raghav Mathur
Ranch Hand

Joined: Jan 12, 2001
Posts: 641
hi
i,am confused about the integral literals .
To express 28 ( which is expressed in decimal ) in octal , we can prefix the literal with
0(zero). To indicate 28 to hexadecimal we prefix it 0X or 0x.
please explain how 28 when expresses on octal becomes 034
and how 28 when expressed in hexadecimal becomes 0x1c.
regards
raghav...

Raghav.
swapna sivaraju
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
Hi

To convert a decimal number to octal we do something like this--
see the representation of 28 in binary is--
let byte b=28;
then,28--00011100
now--split the above into 3 fragaments (for octal) each starting from back..ie.
000 011 100
1 2 3

now do the same as we do to convert the above binary to decimal..ie.
1 represents--0 (2^2*0+2^1*0+2^0*0)
2 represents--3--(2^2*0+2^1*1+2^0*1)
3represents-- 4 (2^2*1+2^1*0+2*2^0*0)
so now that's how we get 034 as octal representation for 28 .
for converting to hexa fragament the binary representation into 4 parts each and calculate.
ie. --0001 1100
1 2
1 ---2^0*1 = 1
2--- 2^3*1+2^2*1+0+0 = 12

and for hexadecimal we represent 12 as c
(10-15 are represented by a-f)
so the hexa value is 0x1c

hope this helps.
swapna

SCPJ2
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by raghav mathur:
hi
i,am confused about the integral literals .
To express 28 ( which is expressed in decimal ) in octal , we can prefix the literal with
0(zero). To indicate 28 to hexadecimal we prefix it 0X or 0x.
please explain how 28 when expresses on octal becomes 034
and how 28 when expressed in hexadecimal becomes 0x1c.
regards
raghav...

We're changing bases here, Raghav. We see 28 in decimal but, in octal, that is 34. You see, to get the value of a number, you take each digit and multiply by the base to the power of the position. Let's look at decimal to begin with:

You can perform a similar operation in the other bases, such as octal (base 8) or hexadecimal (base 16) or binary (base 2):

If you do a search in this forum, I'm sure you'll find all sorts of information on changing base.
Corey

SCJP Tipline, etc.
Raghav Mathur
Ranch Hand

Joined: Jan 12, 2001
Posts: 641
hi
thanks a lot for responding . I,am not very comfortable with the explaination you gave
I've got the thumb rule , that for converting decimal to octal we are suppose to
divide the binary representation of 28 (00011100) into 2 parts . I,am confused
about the calculation which you've done . i.e , how does 1 represents 0 ,
2 represents 3 and 3 represents 4 .
Moreover , convertin decimal to hex , how does 1 represents 1 and 2 represents 12.
regards
raghav mathur
Originally posted by swapna sivaraju:
Hi

To convert a decimal number to octal we do something like this--
see the representation of 28 in binary is--
let byte b=28;
then,28--00011100
now--split the above into 3 fragaments (for octal) each starting from back..ie.
000 011 100
1 2 3

now do the same as we do to convert the above binary to decimal..ie.
1 represents--0 (2^2*0+2^1*0+2^0*0)
2 represents--3--(2^2*0+2^1*1+2^0*1)
3represents-- 4 (2^2*1+2^1*0+2*2^0*0)
so now that's how we get 034 as octal representation for 28 .
for converting to hexa fragament the binary representation into 4 parts each and calculate.
ie. --0001 1100
1 2
1 ---2^0*1 = 1
2--- 2^3*1+2^2*1+0+0 = 12

and for hexadecimal we represent 12 as c
(10-15 are represented by a-f)
so the hexa value is 0x1c

hope this helps.
swapna
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9059

12
please explain how 28 when expresses on octal becomes 034
and how 28 when expressed in hexadecimal becomes 0x1c.

28 / 8 = 3 remainder 4
3 * 8^1 + 4 * 8^0

28 / 16 = 1 remainder 12
1 * 16^1 + 12 (notatated as 'c' in hexadecimal) * 16^0.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
swapna sivaraju
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
oopss.... some typo error..

The representational for 28 is
00011100
the 3 parts are--
1) 000 which is 0
calculated as (0*2^2+0*2^1+0*2^0)
2) 011 which is 3
calculated as (0*2^2+1*2^1+1*2^0)
3) 100 which is 4
calculated as (1*2^2+0*2^1+0*2*2^0)
So now we get the octal form as 034 for 28.got it now??
i hope its clear now
swapna

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

subject: integral literals ?