Advertisement
Guest User

hexaunddec

a guest
Dec 11th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package lab.pkg7;
  2. import java.util.*;
  3.  
  4. public class Lab7 {
  5.  
  6. //John Semanduyev                                     Due December 11, 2018
  7. //CISC 1115 TR11                                                      LAB 7
  8.  
  9.     //method converts hexadecimal to decimal using string as reference
  10.     public static int Hex2Dec(String input) {
  11.         String ref= "0123456789ABCDEF";
  12.         int num=0;
  13.         for (int i=0;i<input.length();i++) {
  14.             char a = input.charAt(i);
  15.             int b= ref.indexOf(a);
  16.             num=16*num+b;
  17.         }
  18.         return num;
  19.     }
  20.    
  21.     //method converts decimal to hexadeciml using array as reference
  22.     public static void Dec2Hex(int z) {
  23.         char[] hexadecimal= {'0','1','2','3','4','5','6','7','8','9','A','B','C'
  24.         ,'D','E','F'};    
  25.         int rem=0;
  26.         String value = "";
  27.         while (z>0) {
  28.             rem=z%16;
  29.             value=hexadecimal[rem]+value;
  30.             z=z/16;
  31.         }
  32.         System.out.println("The hexadecimal value is " +value);
  33.     }
  34.    
  35.     //main program puts it all together and displays everything
  36.     public static void main(String[] args) {
  37.         Scanner input = new Scanner(System.in);
  38.         char s; String in; int num; String holder;
  39.         do{
  40.             System.out.println("Enter a string to convert from "
  41.                     + "hexadecimal to decimal");
  42.             in =input.nextLine();
  43.             System.out.println("The decimal value is " + Hex2Dec(in));
  44.             System.out.println("Enter a number to convert from"
  45.                     + " decimal to hexadecimal");
  46.             num=input.nextInt();
  47.             Dec2Hex(num);
  48.             System.out.println("Enter 's' to go again, any other "
  49.                     + "character to end");
  50.             s=input.next().charAt(0);
  51.             holder=input.nextLine();
  52.         }while(s!='s');
  53.     }
  54. }
  55.  
  56. /*
  57. run:
  58. Enter a string to convert from hexadecimal to decimal
  59. 16A
  60. The decimal value is 362
  61. Enter a number to convert from decimal to hexadecimal
  62. 362
  63. The hexadecimal value is 16A
  64. Enter 's' to go again, any other character to end
  65. f
  66. Enter a string to convert from hexadecimal to decimal
  67. 16A
  68. The decimal value is 362
  69. Enter a number to convert from decimal to hexadecimal
  70. 362
  71. The hexadecimal value is 16A
  72. Enter 's' to go again, any other character to end
  73. f
  74. Enter a string to convert from hexadecimal to decimal
  75. 6754A
  76. The decimal value is 423242
  77. Enter a number to convert from decimal to hexadecimal
  78. 423242
  79. The hexadecimal value is 6754A
  80. Enter 's' to go again, any other character to end
  81. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement