(e.g)if i have decimal 16, how would i know the Octal number is 20 and the Hex number is 10?

Sun Certified Web Component Developer for the J2EE Platform<br />Sun Certified Programmer for the Java 2 Platform

it's very simple just divide ur number with 16 (if it is hex), with 8 (if it is oct). And get the remainder of it.

Example

n is ur number, n = 16

n/16 it will give u 1 and remainder will be 0 so the number is 10.

n/8 it will give u 2 and remainder of course will be 0 so the number would be 20.

I hope this will help u in understanding.

Here is my steps (maybe someone has something better)

Step 1: Take a given value and divide it by the radix value you want to convert it to, the radix for hex is base 16.

Step 2: Take the remainer from step 1 and multiply it by the radix, this will be your next right most digit.

Step 3: Take the integral (whole) value and do step 1 until no more intergral value is left.

Steps to convert 259 to hex value

a. 259 / 16 = 16.1875

remainer is 0.1875 for step b

integral is 16 for step c

b. 1st right most digit = 16 * 0.1875 = 3

c. 16 / 16 = 1.0

remainer is 0 for step d

integral is 1 for step e

d. 2nd right most digit = 16 * 0 = 0

e. 3rd digit = 1

So 259 = 0x103

[ February 18, 2002: Message edited by: Rajinder Yadav ]

<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes

Sheriff

this is a nice solution but allow me to doubt that during the exam one will be willing to perform all those computations. Moreover, your solution does not show how to manipulate bits and this is a very important topic for the purpose of the certification when dealing with shift operators and the like.

A much more elegant solution that all computer scientists, programmers and students should know at the very beginning is to be able to decompose any number in powers of 2. And from there on you can do everything.

I take 259 to show you how easy it is.

1. write the powers of two from the right to the left on a piece of paper, that is 2 power 0, 2 power 1, 2 power 2, and so on:

256 128 64 32 8 4 2 1

2. Decompose your number into those numbers, that is, try to find all powers of two that you can add together and get 259 (there is only one solution). You write a 1 under each number you choose and a 0 under each other number. Easy:

256 128 64 32 16 8 4 2 1

1 0 0 0 0 0 0 1 1

That is 256+2+1 = 259.

100000011 is your binary representation of 259. From there on you can do two things, either convert to hexadecimal (radix 16) or to octal (radix 8).

3. If you want ot convert to hexadecimal, group your binary sequence by groups of 4 bits (because the biggest value a group of 4 bits can have is 15) beginning on the right side and pad the incomplete group on the left with 0's, like this:

0001 0000 0011

Then you just have to take one binary group after another and convert its value to hexadecimal (values range form 0 to F).

So we have 103 which we write 0x103 to signify that the number is in hexadecimal and that's it...

4. If you want ot convert to octal, group your binary sequence by groups of 3 bits (because the biggest value a group of 3 bits can have is 7) beginning on the right side and pad the incomplete group on the left with 0's, like this:

100 000 011

Then you just have to take one binary group after another and convert its value to its octal equivalent (values range from 0 to 7).

So we have 403 which we write 0403 to signify that the number is in octal and that's it...

So 259 in decimal is

- 100000011 in binary

- 0x103 in hexadecimal

- 0403 in octal

You must have noticed by now that the latter three are tightly coupled. Do learn this technique because it is very important to know it.

If you want more information, please read the following link Javaranch:Cat and Mouse Games with Bits or do a search in this forum.

[ February 18, 2002: Message edited by: Valentin Crettaz ]

[ February 18, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML

[Blog] [Blogroll] [My Reviews] My Linked In

Originally posted by Valentin Crettaz:

hex ... (because the biggest value a group of 4 bits can have is 16)

An F is 15 actually (8+4+2+1=15) hexadecimal range is [0-15], but I'm sure that was just a typo.

oct ... (because the biggest value a group of 3 bits can have is 8)

...weeeellllll, that's 7 than (4+2+1=7) octal range is [0-7].

Excellent explanation otherwise.

Peter Bugla<br />Sun Certified Programmer for Java 2 Platform

Now, look back at the remainders and list them in reverse order that you obtained them, in this case:

10011111

This value is 159 in binary. You can then use Val's technique to quickly convert from binary to octal or hexadecimal. You can, however, use this technique to directly convert to octal or hexadecimal, if you'd like, by dividing by 8 or 16. For large numbers, however, I find it easier to convert to binary and then go from there because converting from binary to octal or hexadecimal is very quick and easy.

Corey

Sheriff

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML

[Blog] [Blogroll] [My Reviews] My Linked In

<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes

Now i know how to convert from Decimal ->Octal -> Hex.

What if they give a Hex number and ask me to convert it to Oct or Dec? Can you show me how to do it the other way around?

Sun Certified Web Component Developer for the J2EE Platform<br />Sun Certified Programmer for the Java 2 Platform

Hex-Binary

0-0000

1-0001

2-0010

.

.

9-1001

A-1010

B-1011

C-1100

D-1101

E-1110

F-1111

So, if you get the hex number 0xEF then the binary number is 1110 1111.

So the decimal value is 128+64+32+0+8+4+2+1=239

For octal chart is like this

Octal-Binary

0-000

1-001

2-010

.

.

.

7-111

So, if you get octal number 45 then the binary number is 100 101.

Decimal value is 32+0+0+4+0+1=37

I think it will help you.

-----------------<br />IBM XML Developer <br />SCJP 1.4

Suppose you have 0xFE

Then binary is 1111 1110

If you split it by taking 3 bit in a group form right side then

011 111 110

3 7 6

So, the octal number is 376

From octal to hexadecimal conversion can be done in the similar way.

-----------------<br />IBM XML Developer <br />SCJP 1.4

It is sorta covered in the JavaRanch Style Guide. |