Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!

# conversion

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
How to convert number 1.) 0x65 to hexadecimal and
2.) 065 to octal literal.

ALso how to convert from octal literal back to number.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

I'm not sure I understand: 0x65 is a hexadecimal value. Did you want to convert it to decimal? Here's an example if that's what you want.

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
Sorry

I want it to convert it to decimal.

The link you gave me is the code .

but how can i evaluate for SCJP exam
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You know that each position in a decimal number means a power of 10. Starting from the right, moving to the left, we have the number of ones, then the number of tens, then the number of hundreds, thousands, ten-thousands, and so on. So 4242 is 2 ones plus 4 tens plus 2 hundreds and 4 thousands. That is: 2 + 40 + 200 + 4000, resulting in, yes, 4242. 10 is the base of this decimal system

Hexadecimal has base 16, and octal has base 8.

In a hexadecimal number, going from right to left, you have the number of ones, the number of sixteens, the number of two-hundred-and-fifty-sixes, the number of four-thousand-and-ninety-sixes, and so on. So 0x65 means 5 ones plus 6 sixteens, that is, 5 plus, um..., 96. That makes 101 in decimal. So 0x65 (read it as 65 in hex) is 101 in decimal.

I leave the octal conversion as an exercise. Do not forget that an int literal begining with 0 is octal.

Any modern (high-)school maths book should have a few pages on this topic.

[ September 17, 2005: Message edited by: Barry Gaunt ]

six times sixteen is nintysix not eighty

[ September 17, 2005: Message edited by: Barry Gaunt ]
[ September 17, 2005: Message edited by: Barry Gaunt ]

Getting someone to think and try something out is much more useful than just telling them the answer.
Roy Simon
Ranch Hand

Joined: Aug 26, 2005
Posts: 62
if you find dividing and multiplying by 16 tough

convert the number to binary first
each hexa digit can be represented by four binary bits
eg 6==0110,f=1111and 0=0000

once you have done this
conversion to decimal will be a piece of cake as you can conver the decimal to binary
eg 111 = 1*2^0 + 1*2^1 + 1*2^2 == 7
pls note the ^ stands for power and not XOR

Regards
Simon
Roy Simon
Ranch Hand

Joined: Aug 26, 2005
Posts: 62
to convert to hexa or octal binary rep will work

as the radix for binary is 2
the radix of hexa 16(2^4)-hence each hexa digit can be rep as 4 binary digits
the radix of octal is 8(2^3)-hence each octal bit can be rep as 3 binary bits

Regards
Simon

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

subject: conversion