Two Laptop Bag*
The moose likes Java in General and the fly likes A problem with java literals(Hex & octal values etc) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "A problem with java literals(Hex & octal values etc)" Watch "A problem with java literals(Hex & octal values etc)" New topic

A problem with java literals(Hex & octal values etc)

Femi Alla
Ranch Hand

Joined: Jul 05, 2002
Posts: 79
Hello, I would be sitting for the programmer exam on saturday and there is only one set of questions I would skip 'cos I don't know. It's
a little problem with the declaration of octal and hexadecimal values in java.
The Java 2 certification study guide says on page 11 that
"The value of 28 may thus be expressed six ways:
28// Decimal value.
034//this is supposed to be in base 8,right?
0x1C} Hexadecimal values.
No matter how hard I've tried, I don't seem to understand any except for the first. My calculation of 28 to base 8(the second item on the list) is: 2x81 + 2x80
Which would give; 16 + 2. I'd have expected the result to be 018. But it's 034. The rest I don't even know what to think.
I need help here. Can anyone please help me out?

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
You've got a few things turned around. It's 34 which is already in octal - so to see the equivalent decimal value, calculate
3 * 8^1 + 4 * 8^0 = 24 + 4 = 28
(where of course ^ is "to the power of", not the java OR operator) Likewise 0x1C is in hex, so convert to decimal thus:
1 * 16^1 + 12 * 16^0 = 16 + 12 = 28

"I'm not back." - Bill Harding, Twister
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
On each bite,
1~0 for digital
1~7 for octal
1~10~A~B~C~D~E for hex
I agree. Here's the link:
subject: A problem with java literals(Hex & octal values etc)
Similar Threads
hex into byte
range of char
Java integers three representations
integral literals ?