Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. ```/***************************************************
  2.  
  3. Name: Gorm Osborg
  4. Date: 17/2/2017
  5. Homework #7
  6.  
  7. Program name: HexConversion
  8. Program description: Accepts hexadecimal numbers as input and converts each number to binary
  9. and to the decimal equivalent.
  10. Valid input examples: F00D, 000a, 1010, FFFF, Goodbye, GOODBYE
  11. Enter GOODBYE (case insensitive) to exit the program.
  12.  
  13. ****************************************************/
  14.  
  15. package hw8;
  16.  
  17. import java.util.Scanner;
  18.  
  19. public class HexConversion {
  20.  
  21. public static void main(String[] args) {
  22.  
  23. // Maximum length of input string
  24. final byte INPUT_LENGTH = 4;
  25.  
  26. String userInput = ""; // Initialize to null string
  27. Scanner input = new Scanner(System.in);
  28.  
  29. // Process the inputs until GOODBYE is entered
  30. do {
  31. // Input a 4 digit hex number
  32. System.out.print("\nEnter a hexadecimal string, or enter GOODBYE to quit: ");
  33. userInput = input.next().toUpperCase();
  34.  
  35. // Process the input
  36. switch (userInput) {
  37.  
  38. case "GOODBYE": break;
  39.  
  40. default: if (isValidHex(userInput, INPUT_LENGTH)) {
  41. // The input is a valid hexadecimal string
  42.  
  43. // Convert the hexadecimal string to binary and print the binary number as a string
  44. String binVal = hex2Bin(userInput, INPUT_LENGTH);
  45.  
  46. // Convert the hexadecimal string to decimal and print the decimal number
  47. long decVal = hex2Dec(userInput, INPUT_LENGTH);
  48. System.out.printf(" 0x%s = %s in binary = %d in decimal (unsigned).\n", userInput, binVal, decVal);
  49. }
  50.  
  51. else {
  52. // String is either the wrong length or is not a valid hexadecimal string
  53. System.out.printf(" The string %s is not a valid input.\n", userInput);
  54. }
  55. break;
  56. }
  57. } while (!userInput.equals("GOODBYE"));
  58.  
  59. // Exit the program
  60. System.out.println("\nGoodbye!");
  61. input.close();
  62. }
  63.  
  64. // Method to validate the input
  65. public static boolean isValidHex(String userIn, byte inputLen) {
  66. boolean isValid = false;
  67.  
  68. // If length of the input string is equal to inputLen, continue with validation,
  69. // otherwise return false
  70. if (userIn.length() == inputLen) {
  71.  
  72. // The length is correct, now check that the characters are legal hexadecimal digits
  73. for (int i = 0; i < inputLen; i++) {
  74. char thisChar = userIn.charAt(i);
  75.  
  76. // Is the character a decimal digit (0..9)? If so, advance to the next character
  77. if (Character.isDigit(thisChar)) {
  78. isValid = true;
  79. }
  80.  
  81. else {
  82. // Character is not a decimal digit (0..9), is it a valid hexadecimal digit (A..F)?
  83. if ("ABCDEFabcdef".indexOf(thisChar) != -1) { // Can just make the string "0123456789ABCDEFabcdef" and simplify most of this method
  84. isValid = true;
  85. }
  86. else;
  87. break;
  88. }
  89. break;
  90. }
  91. }
  92.  
  93. // Return true if the string is a valid hexadecimal string, false otherwise
  94. if (isValid == true) {
  95. return true;
  96. }
  97. else {
  98. return false;
  99. }
  100. }
  101.  
  102.  
  103. // Method to convert the hex number to a binary string
  104. public static String hex2Bin(String hexString, byte inputLen) {
  105. String binString = ""; // Initialize binString to null string
  106.  
  107. // Convert each hexadecimal digit to its binary equivalent
  108. for (int i = 0; i < inputLen; i++) {
  109. char thisChar = hexString.charAt(i);
  110. if (thisChar.isDigit %2 == 0) {
  111. thisChar = 1;
  112. }
  113. else;
  114.  
  115.  
  116. // Convert hexString to a binary string, e.g. F00D = 1111000000001101
  117. TBD
  118. }
  119. }
  120.  
  121. // Method to convert the hex number to decimal number
  122. public static long hex2Dec(String hexString, byte inputLen) {
  123.  
  124. // Convert hexadecimal string to decimal, e.g. F00D = 61453 in unsigned decimal
  125. TBD
  126. }
  127. }
  128. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement