Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```/***************************************************
- Name: Gorm Osborg
- Date: 17/2/2017
- Homework #7
- Program name: HexConversion
- Program description: Accepts hexadecimal numbers as input and converts each number to binary
- and to the decimal equivalent.
- Valid input examples: F00D, 000a, 1010, FFFF, Goodbye, GOODBYE
- Enter GOODBYE (case insensitive) to exit the program.
- ****************************************************/
- package hw8;
- import java.util.Scanner;
- public class HexConversion {
- public static void main(String[] args) {
- // Maximum length of input string
- final byte INPUT_LENGTH = 4;
- String userInput = ""; // Initialize to null string
- Scanner input = new Scanner(System.in);
- // Process the inputs until GOODBYE is entered
- do {
- // Input a 4 digit hex number
- System.out.print("\nEnter a hexadecimal string, or enter GOODBYE to quit: ");
- userInput = input.next().toUpperCase();
- // Process the input
- switch (userInput) {
- case "GOODBYE": break;
- default: if (isValidHex(userInput, INPUT_LENGTH)) {
- // The input is a valid hexadecimal string
- // Convert the hexadecimal string to binary and print the binary number as a string
- String binVal = hex2Bin(userInput, INPUT_LENGTH);
- // Convert the hexadecimal string to decimal and print the decimal number
- long decVal = hex2Dec(userInput, INPUT_LENGTH);
- System.out.printf(" 0x%s = %s in binary = %d in decimal (unsigned).\n", userInput, binVal, decVal);
- }
- else {
- // String is either the wrong length or is not a valid hexadecimal string
- System.out.printf(" The string %s is not a valid input.\n", userInput);
- }
- break;
- }
- } while (!userInput.equals("GOODBYE"));
- // Exit the program
- System.out.println("\nGoodbye!");
- input.close();
- }
- // Method to validate the input
- public static boolean isValidHex(String userIn, byte inputLen) {
- boolean isValid = false;
- // If length of the input string is equal to inputLen, continue with validation,
- // otherwise return false
- if (userIn.length() == inputLen) {
- // The length is correct, now check that the characters are legal hexadecimal digits
- for (int i = 0; i < inputLen; i++) {
- char thisChar = userIn.charAt(i);
- // Is the character a decimal digit (0..9)? If so, advance to the next character
- if (Character.isDigit(thisChar)) {
- isValid = true;
- }
- else {
- // Character is not a decimal digit (0..9), is it a valid hexadecimal digit (A..F)?
- if ("ABCDEFabcdef".indexOf(thisChar) != -1) { // Can just make the string "0123456789ABCDEFabcdef" and simplify most of this method
- isValid = true;
- }
- else;
- break;
- }
- break;
- }
- }
- // Return true if the string is a valid hexadecimal string, false otherwise
- if (isValid == true) {
- return true;
- }
- else {
- return false;
- }
- }
- // Method to convert the hex number to a binary string
- public static String hex2Bin(String hexString, byte inputLen) {
- String binString = ""; // Initialize binString to null string
- // Convert each hexadecimal digit to its binary equivalent
- for (int i = 0; i < inputLen; i++) {
- char thisChar = hexString.charAt(i);
- if (thisChar.isDigit %2 == 0) {
- thisChar = 1;
- }
- else;
- // Convert hexString to a binary string, e.g. F00D = 1111000000001101
- TBD
- }
- }
- // Method to convert the hex number to decimal number
- public static long hex2Dec(String hexString, byte inputLen) {
- // Convert hexadecimal string to decimal, e.g. F00D = 61453 in unsigned decimal
- TBD
- }
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement