Hello, for an assignment I have to make a program in Java that converts Hexadeci
ID: 3902898 • Letter: H
Question
Hello, for an assignment I have to make a program in Java that converts Hexadecimal values to decimal, Binary values to decimal and also convert from Binary To Hexadecimal. the sample output is at the end.
This assignment is designed to get you to use loops in conjunction with the standard Java String class, the Java char type, and some arithmetic. Your program will take a hexadecimal or binary number as an input, and then print out its decimal value.
Your program must have a looping menu that prompts the user to choose between hex to decimal decoding, binary to decimal decoding, or binary to hex. Binary to hex is extra credit and is not required to be implemented.
Your program must use methods. The following method signatures must be matched, and implemented and used properly. They must not print anything out.
public static long hexStringDecode(String hex) //Decode whole hex string
public static short hexCharDecode(char hexChar) //Decode single character
public static long binaryStringDecode(String bin) //Decode binary string
public static String binaryToHex(String bin) //Optional
Sample Output:
Choose an option:
1. Decode a hex string.
2. Decode a binary string.
3. Convert binary to hex.
4. Quit.
1
Please enter the hex string:
0x4321
Result: 17185
Choose an option:
1. Decode a hex string.
2. Decode a binary string.
3. Convert binary to hex.
4. Quit.
1
Please enter the hex string:
0xFFFFFFFF
Result: 4294967295
Choose an option:
1. Decode a hex string.
2. Decode a binary string.
3. Convert binary to hex.
4. Quit.
2
Please enter the binary string:
1010
Result: 10
Choose an option:
1. Decode a hex string.
2. Decode a binary string.
3. Convert binary to hex.
4. Quit.
2
Please enter the binary string:
11111111
Result: 255
Choose an option:
1. Decode a hex string.
2. Decode a binary string.
3. Convert binary to hex.
4. Quit.
3
Please enter the binary string:
111110000001
Result: F81
Choose an option:
1. Decode a hex string.
2. Decode a binary string.
3. Convert binary to hex.
4. Quit.
4
Quitting program.
Explanation / Answer
Here is the completed code for this problem including extra credit part. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Converter.java
import java.util.Scanner;
public class Converter {
public static long hexStringDecode(String hex) // Decode whole hex string
{
// converting to upper case
hex = hex.toUpperCase();
// removing '0x' prefix if exists
if (hex.startsWith("0x")) {
hex = hex.substring(2);
}
long res = 0;
// looping through all characters in the string
for (int i = 0; i < hex.length(); i++) {
// getting current character
char c = hex.charAt(i);
// decoding hex value
short value = hexCharDecode(c);
// converting to hexadecimal equivalent and updating res variable
res = 16 * res + value;
}
return res;
}
public static short hexCharDecode(char hexChar) // Decode single character
{
short res = 0;
// converting to upper case to reduce comparisons
hexChar = Character.toUpperCase(hexChar);
// returns digit value for 0-9, then returns corresponding values for
// alphabets A-F
switch (hexChar) {
case '0':
res = 0;
break;
case '1':
res = 1;
break;
case '2':
res = 2;
break;
case '3':
res = 3;
break;
case '4':
res = 4;
break;
case '5':
res = 5;
break;
case '6':
res = 6;
break;
case '7':
res = 7;
break;
case '8':
res = 8;
break;
case '9':
res = 9;
break;
case 'A':
res = 10;
break;
case 'B':
res = 11;
break;
case 'C':
res = 12;
break;
case 'D':
res = 13;
break;
case 'E':
res = 14;
break;
case 'F':
res = 15;
break;
}
return res;
}
public static long binaryStringDecode(String bin) // Decode binary string
{
long res = 0;
int power = bin.length() - 1; // starting power of 2
// looping through all characters in the string
for (int i = 0; i < bin.length(); i++) {
// only considering 'high' values
if (bin.charAt(i) == '1') {
res = (long) (res + Math.pow(2, power));
}
power--;
}
return res;
}
public static String binaryToHex(String bin) // Optional
{
/**
* Converting binary to decimal first, and then converting decimal to
* corresponding hex value
*/
long decimalValue = binaryStringDecode(bin);
String hexValue = decimalToHex(decimalValue);
return hexValue;
}
//Converts decimal value to hex
public static String decimalToHex(long decimal) {
int remainder;
String res = "";
char characters_array[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
while (decimal > 0) {
remainder = (int) (decimal % 16);
res = characters_array[remainder] + res;
decimal = decimal / 16;
}
return res;
}
public static void main(String[] args) {
//Scanner for reading input
Scanner scanner = new Scanner(System.in);
int ch = 0;
String input;
long result;
//loop until user wishes to quit
do {
//showing menu
System.out.println("Choose an option:");
System.out.println("1. Decode a hex string.");
System.out.println("2. Decode a binary string.");
System.out.println("3. Convert binary to hex.");
System.out.println("4. Quit");
ch = Integer.parseInt(scanner.nextLine());
//performing conversions based on choice
switch (ch) {
case 1:
System.out.println("Please enter the hex string:");
input = scanner.nextLine();
result = hexStringDecode(input);
System.out.println("Result: " + result);
break;
case 2:
System.out.println("Please enter the binary string:");
input = scanner.nextLine();
result = binaryStringDecode(input);
System.out.println("Result: " + result);
break;
case 3:
System.out.println("Please enter the binary string:");
input = scanner.nextLine();
String hexString = binaryToHex(input);
System.out.println("Result: " + hexString);
break;
case 4:
System.out.println("Bye!");
break;
default:
System.out.println("Invalid choice");
break;
}
} while (ch != 4);
}
}
/*OUTPUT*/
Choose an option:
1. Decode a hex string.
2. Decode a binary string.
3. Convert binary to hex.
4. Quit
1
Please enter the hex string:
0x4321
Result: 17185
Choose an option:
1. Decode a hex string.
2. Decode a binary string.
3. Convert binary to hex.
4. Quit
1
Please enter the hex string:
EEE1234
Result: 250483252
Choose an option:
1. Decode a hex string.
2. Decode a binary string.
3. Convert binary to hex.
4. Quit
2
Please enter the binary string:
1010
Result: 10
Choose an option:
1. Decode a hex string.
2. Decode a binary string.
3. Convert binary to hex.
4. Quit
2
Please enter the binary string:
111000111
Result: 455
Choose an option:
1. Decode a hex string.
2. Decode a binary string.
3. Convert binary to hex.
4. Quit
3
Please enter the binary string:
1010
Result: A
Choose an option:
1. Decode a hex string.
2. Decode a binary string.
3. Convert binary to hex.
4. Quit
3
Please enter the binary string:
111000110111
Result: E37
Choose an option:
1. Decode a hex string.
2. Decode a binary string.
3. Convert binary to hex.
4. Quit
4
Bye!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.