Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Exam;
- import java.util.Scanner;
- public class returnx
- {
- public static void main(String[] args)
- {
- boolean loop = true;
- while(loop)
- {
- Scanner input = new Scanner(System.in);
- System.out.print("Input number type to be converted: ");
- String answer = input.next();
- switch(answer.toLowerCase())
- {
- case "decimal":
- System.out.println("You chose decimal! Please enter a decimal number: ");
- int original = input.nextInt();
- toBinary(original);
- toOctal(original);
- toHex(original);
- break;
- case "binary":
- System.out.println("You chose binary! Please eneter a binary number: ");
- int original1 = input.nextInt();
- int x2 = fromBinary(original1);
- toOctal(x2);
- toHex(x2);
- break;
- case "octal":
- System.out.println("You chose octal! Please enter an octal number: ");
- int original2 = input.nextInt();
- int x3 = fromOctal(original2);
- toBinary(x3);
- toHex(x3);
- break;
- case "hex":
- System.out.println("You chose hex! Please enter a hex number: ");
- String original3 = input.next();
- int x1 = Integer.parseInt(fromHex(original3));
- toBinary(x1);
- toOctal(x1);
- break;
- case "stop":
- System.out.println("Converter stopped");
- loop = false;
- break;
- }
- }
- }
- public static String toBinary(int original)
- {
- int binary = original;
- String bResult = "";
- int lastBinary = binary % 2;
- String finalResult = "";
- while(binary >= 2)
- {
- binary = binary / 2;
- bResult += Integer.toString(binary % 2);
- }
- System.out.print("Binary: ");
- for(int x = bResult.length() - 1; x >= 0; x--)
- {
- finalResult += bResult.charAt(x);
- }
- finalResult += lastBinary;
- System.out.println(finalResult);
- return finalResult;
- }
- public static String toOctal(int original)
- {
- int octal = original;
- String oResult = "";
- int lastOctal = octal % 8;
- String finalResult = "";
- while(octal >= 2)
- {
- octal = octal / 8;
- oResult += Integer.toString(octal % 8);
- }
- System.out.print("Octal: ");
- for(int x = oResult.length() - 1; x >= 0; x--)
- {
- finalResult += oResult.charAt(x);
- }
- finalResult += lastOctal;
- System.out.println(finalResult);
- return finalResult;
- }
- public static String toHex(int original)
- {
- int hex = original;
- String hResult = "";
- String finalResult = "";
- if(original > 15 && original < 32)
- {
- switch(original)
- {
- case 16:
- hResult += ("01");
- break;
- case 17:
- hResult += ("11");
- break;
- case 18:
- hResult += ("21");
- break;
- case 19:
- hResult += ("31");
- break;
- case 20:
- hResult += ("41");
- break;
- case 21:
- hResult += ("51");
- break;
- case 22:
- hResult += ("61");
- break;
- case 23:
- hResult += ("71");
- break;
- case 24:
- hResult += ("81");
- break;
- case 25:
- hResult += ("91");
- break;
- case 26:
- hResult += ("A1");
- break;
- case 27:
- hResult += ("B1");
- break;
- case 28:
- hResult += ("C1");
- break;
- case 29:
- hResult += ("D1");
- break;
- case 30:
- hResult += ("E1");
- break;
- case 31:
- hResult += ("F1");
- break;
- }
- }
- else
- {
- while(hex >= 2)
- {
- switch(hex % 16)
- {
- case 10:
- hResult += "A";
- break;
- case 11:
- hResult += "B";
- break;
- case 12:
- hResult += "C";
- break;
- case 13:
- hResult += "D";
- break;
- case 14:
- hResult += "E";
- break;
- case 15:
- hResult += "F";
- break;
- default:
- hResult += Integer.toString(hex % 16);
- break;
- }
- hex = hex / 16;
- }
- }
- System.out.print("Hex: ");
- for(int x = hResult.length() - 1; x >= 0; x--)
- {
- finalResult += hResult.charAt(x);
- }
- System.out.println(finalResult);
- return finalResult;
- }
- public static int fromOctal(int original)
- {
- int multiplied = 0;
- int y = 0;
- String fromB = Integer.toString(original);
- for(int x = fromB.length()-1; x >= 0; x--)
- {
- multiplied = multiplied + (Character.getNumericValue(fromB.charAt(x)) * (int)(Math.pow(8, y)));
- y++;
- }
- System.out.println("Decimal: " + multiplied);
- return multiplied;
- }
- public static int fromBinary(int original)
- {
- int multiplied = 0;
- int y = 0;
- String fromB = Integer.toString(original);
- for(int x = fromB.length()-1; x >= 0; x--)
- {
- multiplied = multiplied + (Character.getNumericValue(fromB.charAt(x)) * (int)(Math.pow(2, y)));
- y++;
- }
- System.out.println("Decimal: " + multiplied);
- return multiplied;
- }
- public static String fromHex(String original)
- {
- int multiplied = 0;
- int y = 0;
- String fromB = original;
- for(int x = fromB.length()-1; x >= 0; x--)
- {
- switch(fromB.charAt(x))
- {
- case 'A':
- multiplied = multiplied + (10 * (int)(Math.pow(16, y)));
- break;
- case 'B':
- multiplied = multiplied + (11 * (int)(Math.pow(16, y)));
- break;
- case 'C':
- multiplied = multiplied + (12 * (int)(Math.pow(16, y)));
- break;
- case 'D':
- multiplied = multiplied + (13 * (int)(Math.pow(16, y)));
- break;
- case 'E':
- multiplied = multiplied + (14 * (int)(Math.pow(16, y)));
- break;
- case 'F':
- multiplied = multiplied + (15 * (int)(Math.pow(16, y)));
- break;
- default:
- multiplied = multiplied + (Character.getNumericValue(fromB.charAt(x)) * (int)(Math.pow(16, y)));
- break;
- }
- y++;
- }
- System.out.println("Decimal: " + multiplied);
- return Integer.toString(multiplied);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement