In: Computer Science
Write a program to convert the input numbers to another number system.
1. Decimal to Binary
2. Binary to Decimal
3. Hexadecimal to Decimal
4. Decimal to Hexadecimal
5. Binary to Hexadecimal
6. Hexadecimal to Binary
The user will type in the input number as following:
Binary number : up to 8 bits
Hexadecimal number: up to 2 bytes
Decimal number: Less than 256
As a result, print out the output after the conversion with their input numbers.
The program must repeat the procedure until the user wants to stop the program
Following is the program written in JAVA to perform the conversions.
Code:
==========================================================================================
import java.util.Scanner;
public class NumberConversionDemo {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
NumberConverter nc = new
NumberConverter();
Integer option =
null;
String num = null;
do {
System.out.println("Welcome to the number converter. Please select
a number to execute the option in front of it:\n");
System.out.println("1. Decimal to Binary\n");
System.out.println("2. Binary to Decimal\n");
System.out.println("3. Hexadecimal to Decimal\n");
System.out.println("4. Decimal to Hexadecimal\n");
System.out.println("5. Binary to Hexadecimal\n");
System.out.println("6. Hexadecimal to Binary\n");
System.out.println("Press 0 to exit\n");
option = Integer.parseInt(sc.nextLine());
switch(option) {
case 0: sc.close();
return;
default: System.out.println("Please enter the
number to be converted.\n");
num = sc.nextLine();
System.out.println(nc.convert(num, option));
sc.nextLine();
}
System.out.println("\n\n\n=============================================================================================");
}
while(true);
}
}
class NumberConverter {
private Integer number;
public String convert(String num, Integer
option) {
switch(option) {
case 1: return convertDecToBin(num);
case 2: return convertBinToDec(num);
case 3: return convertHexToDec(num);
case 4: return convertDecToHex(num);
case 5: return convertBinToHex(num);
case 6: return convertHexToBin(num);
}
return "Invalid option.
Please try again.\n";
}
public String convertDecToBin(String num)
{
this.number =
Integer.parseInt(num);
if(this.number > 256)
{
return "Invalid input or number out of range. Please try
again.\n";
}
String revNum =
"";
Integer rem;
while (this.number>0)
{
rem = this.number % 2;
revNum = revNum + "" + rem;
this.number = this.number/2;
}
StringBuilder sb = new
StringBuilder(revNum);
return "Input: " + num +
"\nOutput: " + sb.reverse().toString();
}
public String convertBinToDec(String num)
{
if(num.matches("(.*)[2-9](.*)") || num.length() > 8) {
return "Invalid input or number out of range. Please try
again.\n";
}
return "Input: " + num +
"\nOutput: " + Integer.parseInt(num, 2);
}
public String convertHexToDec(String num)
{
num =
num.toLowerCase();
if(num.matches("(.*)[g-zG-Z](.*)") || num.length() > 4) {
return "Invalid input or number out of range. Please try
again.\n";
}
return "Input: " + num +
"\nOutput: " + Integer.parseInt(num, 16);
}
public String convertDecToHex(String num)
{
this.number =
Integer.parseInt(num);
if(this.number > 256)
{
return "Invalid input or number out of range. Please try
again.\n";
}
return "Input: " + num +
"\nOutput: " + Integer.toHexString(this.number);
}
public String convertBinToHex(String num)
{
if(num.matches("(.*)[2-9](.*)") || num.length() > 8) {
return "Invalid input or number out of range. Please try
again.\n";
}
return "Input: " + num +
"\nOutput: " + Integer.toHexString(Integer.parseInt(num, 2));
}
public String convertHexToBin(String num)
{
num =
num.toLowerCase();
if(num.matches("(.*)[g-zG-Z](.*)") || num.length() > 4) {
return "Invalid input or number out of range. Please try
again.\n";
}
return "Input: " + num +
"\nOutput: " + Integer.toBinaryString(Integer.parseInt(num,
16));
}
}