5 Replies - 9114 Views - Last Post: 13 July 2011 - 07:05 AM Rate Topic: -----

#1 Hailstorm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-July 11

Convert Decimal to Binary, Octal and Hexadecimal

Posted 12 July 2011 - 11:58 PM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: Convert Decimal to Binary, Octal and Hexadecimal

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,203
  • Joined: 21-June 11

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).
Was This Post Helpful? 1
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: Convert Decimal to Binary, Octal and Hexadecimal

Posted 13 July 2011 - 05:32 AM

View Postsepp2k, 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)
Was This Post Helpful? 1
  • +
  • -

#4 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

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 base-10, base-2, base-8, and base-16, respectively. Now, these numbers in the "base-X" 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 base-10 system again.

Using the number from before, 15, let's say we were making the most useless program in the world, to convert base-10 numbers to base-10. 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 base-x 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:
Posted Image

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

Was This Post Helpful? 3
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,766
  • Joined: 27-December 08

Re: Convert Decimal to Binary, Octal and Hexadecimal

Posted 13 July 2011 - 06:34 AM

NeoTifa also has a good tutorial on number base conversions.
Was This Post Helpful? 0
  • +
  • -

#6 Hailstorm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-July 11

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!!.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1