Hello guys, can someone help me.. our task is to input a Decimal Number and the output will be Binary,Octal and Hexadecimal..we can use Scanner, BufferedReader of JOptionPane but i prefer JOptionPane to use.. i would appreciate it very much!.tnx a lot.
Convert Decimal to Binary, Octal and Hexadecimal
Page 1 of 15 Replies  10435 Views  Last Post: 13 July 2011  07:05 AM
Replies To: Convert Decimal to Binary, Octal and Hexadecimal
#2
Re: Convert Decimal to Binary, Octal and Hexadecimal
Posted 13 July 2011  05:26 AM
You can use Integer.parseInt(String) to turn a decimal string into an int. You can then use Integer.toString(int, int) to turn that int into a binary, octal or hexadecimal string (using the second argument to specify the radix).
#3
Re: Convert Decimal to Binary, Octal and Hexadecimal
Posted 13 July 2011  05:32 AM
sepp2k, on 13 July 2011  05:26 AM, said:
You can use Integer.parseInt(String) to turn a decimal string into an int. You can then use Integer.toString(int, int) to turn that int into a binary, octal or hexadecimal string (using the second argument to specify the radix).
Or he could use one of the methods
Integer.toBinaryString(int i)
Integer.toHexString(int i)
Integer.toOctalString(int i)
OR you could make a method with the math yourself (which I recommend since you will learn more this way)
#4
Re: Convert Decimal to Binary, Octal and Hexadecimal
Posted 13 July 2011  05:50 AM
Edit: I'm making the assumption that you're doing this for a school project, and any of the methods the other two above me mentioned are forbidden by your professor, if they are, then read the....short essay I wrote below. If you are allowed to use those methods...well, there isn't a problem anymore I guess.
So yeah, I'm gonna math bomb ya a little bit here...but...this is something kinda important that people aren't learning anymore when being taught to program. To be blunt, you need to understand the below mathematical principle before you can do this project, and if you already do understand it, you can code the logic and show us for help where you might need it.

Well, if you understand the concept of these number systems (and aren't allowed to just use one of the functions/libraries I', sure Java has) then doing the program should be simple.
Decimal, Binary, Octal, and Hexadecimal are also known as base10, base2, base8, and base16, respectively. Now, these numbers in the "baseX" notation refer to how many "options" there are for a giving place in a number, shown below:
Base 2  0, 1
Base 8  0, 1, 2, 3, 4, 5, 6, 7
Base 10  0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Base 16  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
So for instance, in the base 10 number "15", there are two "places", the ones spot (taken by the 5), and the tens spot (taken by the 1), which means there is "1 ten" (10) and "5 ones" (5). This is the basis of the human counting system we all use, and as with other things such as walking, we never think about it when we do it.
The same can be used for other number systems, take the binary number 11 for instance. The ones spot is taken by the number 1 (1 in binary), and the twos spot is taken by the number 1 (2 in binary), therefore if you convert this number to decimal, it is 3. Now, the way we get that translation is simple, and to explain it, I'll go to the base10 system again.
Using the number from before, 15, let's say we were making the most useless program in the world, to convert base10 numbers to base10. If you were to put 15 into the program, the program would calculate that the answer would be the sum of 10 + 5. If the number was 25, it would calculate the answer to be the sum of 20 + 5. You probably see why it does this by now, there are "two tens" and "five ones". This is the same concept in all of the basex number systems, and a "value" for each place can be found with the following formula:
base ^ placeNumber (where base is the base number (2, 1, 8, 10, etc), ^ is exponentiation, and placeNumber is the "location" of the single symbol in the number.
Here's an excel table I took a screenshot of cause I was too lazy to make an ascii one here:
The "place number" starts at 0 and proceeds in incrementing from right to left, so just using the hexadecimal number from before and the formula I said before, 120F translates into decimal as ((16 ^ 3) * 1) + ((16 ^ 2) * 2) + ((16 ^ 1) * 0) + ((16 ^ 0) * 15), which simplifies first to (4096 * 1) + (256 * 2) + (16 * 0) + (1 * 15), then to 4096 + 512 + 0 + 15, and finally to 4623
The concept works the other way too, let's try with a small decimal number cause it's 9am, and the above was too much typing for me to do again. Converting the number 24 to binary. First you need to calculate enough of the "places" in binary to fit this decimal number. 2^0 is 1, 2^1 is 2, 2^2 is 4, 2^3 is 8, 2^4 is 16, 2^5 is 32.
24 fits in between 2^4 and 2^5, so that means the number will be 5 chars long. Next we take the decimal number and subtract it by the largest value possible in the list above without going below 0, in this case 2^4. 24  16 is 8. Now we take the 8 and do the same thing, in this case, the 8 fits perfectly with the 2^3 place, making the number 11000.
And that's the basic concept of number systems.
So yeah, I'm gonna math bomb ya a little bit here...but...this is something kinda important that people aren't learning anymore when being taught to program. To be blunt, you need to understand the below mathematical principle before you can do this project, and if you already do understand it, you can code the logic and show us for help where you might need it.

Well, if you understand the concept of these number systems (and aren't allowed to just use one of the functions/libraries I', sure Java has) then doing the program should be simple.
Decimal, Binary, Octal, and Hexadecimal are also known as base10, base2, base8, and base16, respectively. Now, these numbers in the "baseX" notation refer to how many "options" there are for a giving place in a number, shown below:
Base 2  0, 1
Base 8  0, 1, 2, 3, 4, 5, 6, 7
Base 10  0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Base 16  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
So for instance, in the base 10 number "15", there are two "places", the ones spot (taken by the 5), and the tens spot (taken by the 1), which means there is "1 ten" (10) and "5 ones" (5). This is the basis of the human counting system we all use, and as with other things such as walking, we never think about it when we do it.
The same can be used for other number systems, take the binary number 11 for instance. The ones spot is taken by the number 1 (1 in binary), and the twos spot is taken by the number 1 (2 in binary), therefore if you convert this number to decimal, it is 3. Now, the way we get that translation is simple, and to explain it, I'll go to the base10 system again.
Using the number from before, 15, let's say we were making the most useless program in the world, to convert base10 numbers to base10. If you were to put 15 into the program, the program would calculate that the answer would be the sum of 10 + 5. If the number was 25, it would calculate the answer to be the sum of 20 + 5. You probably see why it does this by now, there are "two tens" and "five ones". This is the same concept in all of the basex number systems, and a "value" for each place can be found with the following formula:
base ^ placeNumber (where base is the base number (2, 1, 8, 10, etc), ^ is exponentiation, and placeNumber is the "location" of the single symbol in the number.
Here's an excel table I took a screenshot of cause I was too lazy to make an ascii one here:
The "place number" starts at 0 and proceeds in incrementing from right to left, so just using the hexadecimal number from before and the formula I said before, 120F translates into decimal as ((16 ^ 3) * 1) + ((16 ^ 2) * 2) + ((16 ^ 1) * 0) + ((16 ^ 0) * 15), which simplifies first to (4096 * 1) + (256 * 2) + (16 * 0) + (1 * 15), then to 4096 + 512 + 0 + 15, and finally to 4623
The concept works the other way too, let's try with a small decimal number cause it's 9am, and the above was too much typing for me to do again. Converting the number 24 to binary. First you need to calculate enough of the "places" in binary to fit this decimal number. 2^0 is 1, 2^1 is 2, 2^2 is 4, 2^3 is 8, 2^4 is 16, 2^5 is 32.
24 fits in between 2^4 and 2^5, so that means the number will be 5 chars long. Next we take the decimal number and subtract it by the largest value possible in the list above without going below 0, in this case 2^4. 24  16 is 8. Now we take the 8 and do the same thing, in this case, the 8 fits perfectly with the 2^3 place, making the number 11000.
And that's the basic concept of number systems.
This post has been edited by Tarkenfire: 13 July 2011  05:52 AM
#5
Re: Convert Decimal to Binary, Octal and Hexadecimal
Posted 13 July 2011  06:34 AM
#6
Re: Convert Decimal to Binary, Octal and Hexadecimal
Posted 13 July 2011  07:05 AM
ok, thank u guys a lot..i just read ur replies.. i'll try all of ur suggestions and see what will work on me..i'll let u know..thanks again!!.
Page 1 of 1
