Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to create a simple Hexadecimal to Decimal converter for an old assignment and Regardless of how much I surf the web and read the textbook I cannot figure this assignment out for the life of me.

Here is the given code:

/***************************************************

  Name: 
  Date: 
  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.TBD == inputLen) {
        
      // The length is correct, now check that the characters are legal hexadecimal digits
      for (int i = 0; i < TBD; i++) {
        char thisChar = userIn.TBD;
          
        // 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)?
          TBD
        }
      }
    }
        
    // Return true if the string is a valid hexadecimal string, false otherwise
    TBD
  }

    
  // 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 < TBD; i++) {
      char thisChar = hexString.TBD;
	        
      // 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
  }
}


What I have tried:

I have tried converting the samples in the textbook to this assignment but I do not understand it. I am very desperate and need to finish these assignments as a part of an incomplete from last semester. Please help me out and fill in and explain the TBD sections? Thanks
Posted
Updated 18-Dec-17 19:49pm
Comments
Richard MacCutchan 19-Dec-17 4:02am    
You do not "convert hex to binary" or anything else. Just read the hex digits and convert them to a valid integer. Only when you print them out do you need them in binary, decimal, or any other format.

1 solution

Quote:
Regardless of how much I surf the web and read the textbook I cannot figure this assignment out

First of all Hexadecimal to decimal or binary conversion is mathematics, not magic. You need to understand 'radix', 'bases' and 'bases conversions'.
Radix - Wikipedia[^]
Looks like you also need to learn how to use search engines, because it is a classical problem and there is literally millions of answers in any language.
Quote:
explain the TBD sections?
Java
TBD

To Be Done ? in other words 'your homework', fill the holes.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900