Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab.pkg7;
- import java.util.*;
- public class Lab7 {
- //John Semanduyev Due December 11, 2018
- //CISC 1115 TR11 LAB 7
- //method converts hexadecimal to decimal using string as reference
- public static int Hex2Dec(String input) {
- String ref= "0123456789ABCDEF";
- int num=0;
- for (int i=0;i<input.length();i++) {
- char a = input.charAt(i);
- int b= ref.indexOf(a);
- num=16*num+b;
- }
- return num;
- }
- //method converts decimal to hexadeciml using array as reference
- public static void Dec2Hex(int z) {
- char[] hexadecimal= {'0','1','2','3','4','5','6','7','8','9','A','B','C'
- ,'D','E','F'};
- int rem=0;
- String value = "";
- while (z>0) {
- rem=z%16;
- value=hexadecimal[rem]+value;
- z=z/16;
- }
- System.out.println("The hexadecimal value is " +value);
- }
- //main program puts it all together and displays everything
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- char s; String in; int num; String holder;
- do{
- System.out.println("Enter a string to convert from "
- + "hexadecimal to decimal");
- in =input.nextLine();
- System.out.println("The decimal value is " + Hex2Dec(in));
- System.out.println("Enter a number to convert from"
- + " decimal to hexadecimal");
- num=input.nextInt();
- Dec2Hex(num);
- System.out.println("Enter 's' to go again, any other "
- + "character to end");
- s=input.next().charAt(0);
- holder=input.nextLine();
- }while(s!='s');
- }
- }
- /*
- run:
- Enter a string to convert from hexadecimal to decimal
- 16A
- The decimal value is 362
- Enter a number to convert from decimal to hexadecimal
- 362
- The hexadecimal value is 16A
- Enter 's' to go again, any other character to end
- f
- Enter a string to convert from hexadecimal to decimal
- 16A
- The decimal value is 362
- Enter a number to convert from decimal to hexadecimal
- 362
- The hexadecimal value is 16A
- Enter 's' to go again, any other character to end
- f
- Enter a string to convert from hexadecimal to decimal
- 6754A
- The decimal value is 423242
- Enter a number to convert from decimal to hexadecimal
- 423242
- The hexadecimal value is 6754A
- Enter 's' to go again, any other character to end
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement