This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes octal, hex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "octal, hex" Watch "octal, hex" New topic
Author

octal, hex

Brian Smith
Ranch Hand

Joined: Oct 26, 2002
Posts: 232
hi,
Can anyone please explain how to convert a number to octal & hex.
Thx
sasank manohar
Ranch Hand

Joined: Feb 14, 2008
Posts: 186
A) To convert a number 10 to octal (0-7)
1. Divide the number by 8 you will get 1 and reminder is 2

10/8=1 , 10%8=2
2. Just combine 1 and 2 that is 12

B) To convert a number 20 to hexadecimal(1-F)
Apply same steps as used for octal conversion(Step 1 and 2 above)
20/16=1 , 20%16=4
So Hex representation of 20 is 14.
C) To convert oct to dec
1*8^1 + 2*8^0=10
D) To covert hex to dec
1*16^1 + 4*16^0=20

[This message has been edited by sdev (edited August 15, 2000).]


"SCJP5 | SCWCD5| DEVELOPER"
Brian Smith
Ranch Hand

Joined: Oct 26, 2002
Posts: 232
Thx sdev,
It is clear now.
Brian Smith
Ranch Hand

Joined: Oct 26, 2002
Posts: 232
sdev,
Can u throw some light on these operators >>, >>>, << as well.
It will be great help.
Brian Smith
Ranch Hand

Joined: Oct 26, 2002
Posts: 232
help........
sasank manohar
Ranch Hand

Joined: Feb 14, 2008
Posts: 186
As you may know
>> - signed right shift(divide by 2)
>>> - unsigned right shift
<< - signed left shift(multiply by 2)<br /> <br /> >> - This operator shifts the bits to the right 1 by 1 and
Shifted off bits are lost(discarded) and 0 is brought
into the left(higher order bit).
For a negative number the sign remains and 1 is brought
into the left.
Note: Shifting -1 to the right always results in -1
<< - This operator shifts the bits to the left 1 by 1 and<br /> higher order bit which is shifted out is lost(discarded)<br /> and a 0 is brought into the right<br /> <br /> >>> - This operator shifts the bits to the right 1 by 1 and
higher order bit which is shifted out is lost and a 0
bit is brought into the right (for both +ve and -ve nos.)
Note: This operator always returns +ve value for -ve
values also

This is a brief explanation. If you really want to understand these operators , then you need to work out applying these operators for different type of values
If you have any doubts on a particular topic then you go to search in this top of the forum and type the related topic , you will get lot of answers that were discussed in this forum(just a suggestion).

thanks..
[This message has been edited by sdev (edited August 15, 2000).]
daryl olson
Ranch Hand

Joined: Aug 15, 2000
Posts: 36
The method sdev outlined works for decimal values below 80 but for values above that it there is a little bit more to do.
Consider what the numbers represent. For example:
1) The octal (base 8) number 02134 in base 10 is:
(0* (8^4) + (2*(8^3)) + (1*(8^2)) + (3*(8^1) ) + (4*(8^0)) = 1116
2) So to convert the base 10 number 1116 to octal, reverse the process:
a) 1116/(8^4) < 1 so 0 with 1116 remainder
b) 1116/(8^3) = 2 with (1116 - 1024) = 92 remainder
c) 92/(8^2) = 1 with (92 - 64) = 28 remainder
d) 28/(8^1) = 3 with (28 - 24) = 4 remainder
e) 4/(8^0) = 4
Putting the values in their position, you get: 02134
Same logic applies for hex (base 16) and binary (base 2) numbers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: octal, hex
 
Similar Threads
Numerical conversions in SCJP exam?
Octal
Hex and Octal
char c='\101'; ..?
Maximum digits allowed for each literal