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.

Could someone show me how to get octal and hexadecimal numbers from decimal ones ? eg. How would you go about getting the octal of 489 and its hexadecimal ? I really appreciate your help.

Probably the simplest way is to use the Integer wrapper class. For example:

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher

I think that he means by hand. Octal uses a ones place, and 8's place, a 64's place, 512 etc. Basically you just need to find out how many of each that you need. To convert 489 decimal to octal 512's = 0 too big 64's = 7 divide 489 by 64 = 7 remainder 41 8's = 5 divide 41 by 8 = 5 remainder 1 1's = 1 So 489 decimal = 751 octal. For hex you have 1's, 16's, 256's, 4096's etc. 256's = 1 divide 489 by 256 = 1 remainder 233 16's = 14 (represented by "e") divide 233 by 16 = 14 remainder 9 1's = 9 So 489 decimal = 1e9 hex [ June 26, 2003: Message edited by: Cindy Glass ]

"JavaRanch, where the deer and the Certified play" - David O'Meara

Originally posted by Cindy Glass: To convert 489 decimal to octal 512's = 0 too big 64's = 7 divide 489 by 64 = 7 remainder 41 8's = 5 divide 41 by 8 = 5 remainder 1 1's = 1 So 489 decimal = 751 octal.

I learned a different method for converting decimal numbers to other bases--repeated division by the target base:

You repeat the division until the remainder is less than the divisor. Then construct the number by going back up the remainders (7, 5, 1).

Richard
N 37 33 W 122 18

Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

posted

0

I think that he means by hand. Just tryin' to make life a litte easier.

William Alldred
Greenhorn

Joined: Jun 26, 2003
Posts: 4

posted

0

Originally posted by Cindy Glass: I think that he means by hand. Octal uses a ones place, and 8's place, a 64's place, 512 etc. Basically you just need to find out how many of each that you need. To convert 489 decimal to octal 512's = 0 too big 64's = 7 divide 489 by 64 = 7 remainder 41 8's = 5 divide 41 by 8 = 5 remainder 1 1's = 1 So 489 decimal = 751 octal. For hex you have 1's, 16's, 256's, 4096's etc. 256's = 1 divide 489 by 256 = 1 remainder 233 16's = 14 (represented by "e") divide 233 by 16 = 14 remainder 9 1's = 9 So 489 decimal = 1e9 hex [ June 26, 2003: Message edited by: Cindy Glass ]

William Alldred
Greenhorn

Joined: Jun 26, 2003
Posts: 4

posted

0

Originally posted by Michael Morris: I think that he means by hand. Just tryin' to make life a litte easier.

yes, I meant by hand. I believe I will need to in the Programmer exam.

Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

posted

0

Originally posted by William Alldred:

yes, I meant by hand. I believe I will need to in the Programmer exam.

I doubt it, unless it's changed significantly. But any programmer should be able to make these conversions without breakin' a sweat.

William Alldred
Greenhorn

Joined: Jun 26, 2003
Posts: 4

posted

0

Originally posted by Michael Morris:

I doubt it, unless it's changed significantly. But any programmer should be able to make these conversions without breakin' a sweat.

I seem to remember some practice questions which would require a conversion in order to find the correct answer. However, I hope to God I'm wrong !