GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes One more time, Octal Literals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "One more time, Octal Literals" Watch "One more time, Octal Literals" New topic
Author

One more time, Octal Literals

leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
I've searched the web, this forum and still can't find a simple explanation of converting Decimal to Octal. Most of the www links on Google that look promising, don't work.
As I understand it, all Octal numbers are prefixed with a '0'. What really gripes me is that most of the web resources just say to "add one" when you get past the decimal number '7'. Add one to where?
Can't I just divide the decimal number by 8 and then add the remainder at the far right?
i.e.:
1 = 01
2 = 02
...
7 = 07
8 = ((8/8) + remainder of '0') = 010
16= 020
24= 030
31= 037
100 = 0124
Is this right to assume this?
[ May 15, 2003: Message edited by: leo donahue ]

Thanks, leo
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Looks like you've got it, yes. It's just like adding numbers in decimal, except that you carry results when they reach 8 rather than at 10.


"I'm not back." - Bill Harding, Twister
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

You've almost got it.
100 is not 0124 as you would get with your method (which works for numbers up to 64), it's 0144
The general rule of thumb for converting from decimal to base x is as follows:
Divide the decimal by the highest power of x that is less than a decimal. Write down the integer part of the result, lop it off, and use the remainder with the next-lowest power of x. Stop when you get power of x = 0.
Sound confusing? Yes, so here's an example:
Convert 100 to base 8.
8^2 = 64; 8^3 = 512. We start with 64, since it is less than 100.
100 / 64 = 1.5625 ( 1 r 36). Write down the 1 and use the remainder to repeat for the next lower power of 8 (which is 8^1, or 8)
36 / 8 = 4.5 ( 4 r 4) Write down the 4 (now our number is 14) and use the remainder to repeat for the next lower power of 8. The next lower power is 8^0, or 1.
4 / 1 = 4 (we could have just written down the remainder without carrying out this step, of course.... ) so now we just write down the 4 coming up with the result 144.

Try this for 71.
71 / 64 = 1.109375 (1 r7)
7 / 8 = 0.875 (0 r7)
7 / 1 = 7.0
Result? 107; therefore, 71 in base 10 is 107 in base 8.
You can now use this to prove that Christmas == Halloween. (DEC 25 = OCT 31)
[ May 15, 2003: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

BTW, To reconstruct the decimal you work backwards. 124 base 8 is
1 * (8^3) + 2 * (8^1) + 4 * (8^0)
1 * 64 + 2 * 8 + 4 * 1
64 + 16 + 4
84
[RANDOM DIGRESSION]
84, BTW, is the name of the town in which I grew up. Except the Post Office spelled it out, "Eighty Four"
[/RANDOM DIGRESSION]
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
Thanks guys,
BTW:
--------------------
Joel McNary
"botrus pros botrun pepainetai"
Jim Yingst
"Uva uvam vivendo varia fit."
That's Latin, darlin'! It appears Mr. Ringo is an educated man. Now I really hate him!
 
GeeCON Prague 2014
 
subject: One more time, Octal Literals