# Number Systems Conversion

Page 1 of 1

## 9 Replies - 4221 Views - Last Post: 22 February 2013 - 04:03 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=310946&amp;s=896c0a139343e7d74a19dff11124c547&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Quang Pham

Reputation: -1
• Posts: 74
• Joined: 18-November 12

# Number Systems Conversion

Posted 02 February 2013 - 03:25 PM

I'm Quang Pham, a computer science student at Palomar College. I'm having trouble developing a number systems conversion program in Java. This is what I've written so far:
```/**
* Write a description of class Decimal here.
*
* @Quang Pham
* @1/31/13
*/
import java.swing;

import java.util.Scanner;

public class Decimal
{

public void getDec()
{
String s1;
s1 = JOptionPane.showInputDialog("Please enter the Decimal number you wish to convert.");
Scanner in1 = new Scanner(s1);
inDec = in1.nextInt();
}
public void toBin();
public void toHex();
public void outBin();
public void outHex();

}

```
Does anybody know how to make the conversions? Thanks for any help.

Is This A Good Question/Topic? 0

## Replies To: Number Systems Conversion

### #2 Martyr2

• Programming Theoretician

Reputation: 4819
• Posts: 13,091
• Joined: 18-April 07

## Re: Number Systems Conversion

Posted 02 February 2013 - 04:12 PM

I suppose you are not allowed to use the toHexString()/toBinaryString()/toOctalString() functions?

### #3 Quang Pham

Reputation: -1
• Posts: 74
• Joined: 18-November 12

## Re: Number Systems Conversion

Posted 02 February 2013 - 04:31 PM

Thanks for giving me the functions idea. I will ask the instructor, but I think he said that we couldn't. This would be a faster method. Thanks for your reply to my guestion.

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11393
• Posts: 42,930
• Joined: 27-December 08

## Re: Number Systems Conversion

Posted 02 February 2013 - 08:38 PM

Check out NeoTifa's tutorial Number Systems for Beginners.

### #5 Quang Pham

Reputation: -1
• Posts: 74
• Joined: 18-November 12

## Re: Number Systems Conversion

Posted 03 February 2013 - 04:43 PM

Thanks for the reference to the tutorial. It was very helpful. Now I need to put it into Java code. I need to convert decimal, binary, and hexadecimal, writing in Java code.

Thanks for the reference to the tutorial. It was very helpful. Now I need to put it into Java code. I need to convert decimal, binary, and hexadecimal, writing in Java code.

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11393
• Posts: 42,930
• Joined: 27-December 08

## Re: Number Systems Conversion

Posted 03 February 2013 - 04:45 PM

Glad I could help! Good luck!

### #7 Quang Pham

Reputation: -1
• Posts: 74
• Joined: 18-November 12

## Re: Number Systems Conversion

Posted 19 February 2013 - 03:27 PM

Thank you for your replies for my number systems conversion question. This is the code I have developed so far.
```/**
* Converts decimal numbers to binary and hexadecimal.
*
* @Quang Pham
* @1/31/13
*/
import javax.swing.*;

import java.util.Scanner;

//public class LineWriter()
//{
//}

public class Decimal

{
LineWriter lw;
public Decimal (LineWriter lw)
{
lw = new LineWriter("csis.txt");
}

int inDec;
String Bin;
String Hex;
int number;
int remainder;
char digit;
char A,B,C,D,E,F;

public void getDec()
{

String s1;
s1 = JOptionPane.showInputDialog("Please enter the Decimal number you wish to convert.");
Scanner in1 = new Scanner(s1);
inDec = in1.nextInt();
number = inDec;
System.out.println(" ");
}

public void DecToBin()
{
int i;
String Bin = "";
String reversednumber = "";

while (number != 0)
{
remainder = number % 2;
number = number / 2;
Bin = Bin + remainder;

if(remainder > 0)
{System.out.print ("1");}
else
{System.out.print ("0");}
}

for (i = Bin.length()-1; i >= 0; i--)
reversednumber = reversednumber + Bin.charAt(i);

Bin = reversednumber;

System.out.println ("");
System.out.println (("The decimal entry is:  ") + inDec);
System.out.println (("The binary conversion is:  ") + Bin);
}

public void DecToHex()
{
int i;
int digit = 0;
String Hex = "";
String reversednumber = "";

while (number != 0)
{
remainder = number % 16;
number = number / 16;

if (remainder == 1) {digit = 1;}
if (remainder == 2) {digit = 2;}
if (remainder == 3) {digit = 3;}
if (remainder == 4) {digit = 4;}
if (remainder == 5) {digit = 5;}
if (remainder == 6) {digit = 6;}
if (remainder == 7) {digit = 7;}
if (remainder == 8) {digit = 8;}
if (remainder == 9) {digit = 9;}
if (remainder == 10) {digit = A;}
if (remainder == 11) {digit = B;}
if (remainder == 12) {digit = C;}
if (remainder == 13) {digit = D;}
if (remainder == 14) {digit = E;}
if (remainder == 15) {digit = F;}
Hex = Hex + digit;

for (i = Hex.length()-1; i >= 0; i--)
reversednumber = reversednumber + Hex.charAt(i);

Hex = reversednumber;

}
System.out.println (("The decimal entry is:  ") + inDec);
System.out.println (("The hexadecimal conversion is:  ") + Hex);
}

public void outBin()
{
System.out.println (inDec);
System.out.println (Bin);
}

public void outHex()
{
System.out.println (inDec);
System.out.println (Hex);
}

}

```

I'm still working on my binary and hexadecimal conversions.

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,955
• Joined: 06-March 08

## Re: Number Systems Conversion

Posted 20 February 2013 - 05:59 AM

```           if(remainder > 0)
{System.out.print ("1");}
else
{System.out.print ("0");}

```

and
```            if (remainder == 1) {digit = 1;}
if (remainder == 2) {digit = 2;}
if (remainder == 3) {digit = 3;}
if (remainder == 4) {digit = 4;}
if (remainder == 5) {digit = 5;}
if (remainder == 6) {digit = 6;}
if (remainder == 7) {digit = 7;}
if (remainder == 8) {digit = 8;}
if (remainder == 9) {digit = 9;}
if (remainder == 10) {digit = A;}
if (remainder == 11) {digit = B;}
if (remainder == 12) {digit = C;}
if (remainder == 13) {digit = D;}
if (remainder == 14) {digit = E;}
if (remainder == 15) {digit = F;}

```

What will you do with this when they wil;l ask you to convert to base 64 ?
use
```   String num = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
digit = num.chatAt(remainder);

```

it will work for any base from 2 to 36

### #9 Quang Pham

Reputation: -1
• Posts: 74
• Joined: 18-November 12

## Re: Number Systems Conversion

Posted 21 February 2013 - 06:58 PM

I am having a problem with the decimal to hexadecimal conversion. Only the Java library code seems to work so far. Do you see something wrong with the decimal to hexadecimal code?
```
/**
* Converts decimal to binary and hexadecimal numbers.
*
* @Quang Pham
* @1/31/13
*/
import javax.swing.*;

import java.util.Scanner;

public class Decimal

{
LineWriter lw;
public Decimal (LineWriter lw)
{
lw = new LineWriter("csis.txt");
}

int inDec;
String Bin;
String Hex;
int number;
int remainder;

public void getDec()
{

String s1;
s1 = JOptionPane.showInputDialog("Please enter the Decimal number you wish to convert.");
Scanner in1 = new Scanner(s1);
inDec = in1.nextInt();
number = inDec;
System.out.println(" ");
}

public void DecToBin()
{
int i;
String Bin = "";
String reversednumber = "";

while (number != 0)
{
remainder = number % 2;
number = number / 2;
Bin = Bin + remainder;

if(remainder > 0)
{System.out.print ("1");}
else
{System.out.print ("0");}
}

for (i = Bin.length()-1; i >= 0; i--)
reversednumber = reversednumber + Bin.charAt(i);

Bin = reversednumber;

System.out.println ("");
System.out.println (("The decimal entry is:  ") + inDec);
System.out.println (("The binary conversion is:  ") + Bin);
}

public void DecToHex()
{
String Hex = "";
int dec = 0;
int remainder = 0;
int i = 0;
int j = 0;
String reversednumber = "";
char[] HexDigit = {'0', '1', '2', '3', '4', '5', '6' , '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char A = 'a'; char B = 'b'; char C = 'c'; char D = 'd'; char E ='e'; char F = 'f';

while (number != 0)
{
remainder = number % 16;
number = number / 16;

if (remainder == 1)  HexDigit[0] = 0;
if (remainder == 2)  HexDigit[1] = 1;
if (remainder == 3)  HexDigit[2] = 2;
if (remainder == 4)  HexDigit[3] = 3;
if (remainder == 5)  HexDigit[4] = 4;
if (remainder == 6)  HexDigit[5] = 5;
if (remainder == 7)  HexDigit[6] = 6;
if (remainder == 8)  HexDigit[7] = 7;
if (remainder == 9)  HexDigit[8] = 8;
if (remainder == 10) HexDigit[9] = 9;
if (remainder == 11) HexDigit[10] = A;
if (remainder == 12) HexDigit[11] = B;
if (remainder == 13) HexDigit[12] = C;
if (remainder == 14) HexDigit[13] = D;
if (remainder == 15) HexDigit[14] = E;
if (remainder == 16) HexDigit[15] = F;

Hex = Hex + HexDigit[j];

}

for (i = Hex.length()-1; i >= 0; i--)
reversednumber = reversednumber + Hex.charAt(i);

Hex = reversednumber;
dec = inDec;

String hex = Integer.toHexString(dec);

System.out.println (("The decimal entry is:  ") + inDec);
System.out.println (("The hexadecimal conversion is:  ") + Hex);
System.out.println (("The hexadecimal conversion is (using library method):  ") + hex);
}

public void outBin()
{
System.out.println (inDec);
System.out.println (Bin);
}

public void outHex()
{
System.out.println (inDec);
System.out.println (Hex);
}

}

```

This post has been edited by pbl: 22 February 2013 - 03:59 PM
Reason for edit:: Fixed code tags

### #10 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,955
• Joined: 06-March 08

## Re: Number Systems Conversion

Posted 22 February 2013 - 04:03 PM

Did you read the solutions we posted ?

To convert from binary (which is the base used by any number on a computer) you simply:

n = number % base;
number /= base;
char asciiRepresentation = num.charAt(n);