Advertisement
ellesehc

DecBinOctHex Converter

Oct 17th, 2015
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.57 KB | None | 0 0
  1. package Exam;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class returnx
  6. {
  7.  
  8.     public static void main(String[] args)
  9.     {
  10.         boolean loop = true;
  11.         while(loop)
  12.         {
  13.         Scanner input = new Scanner(System.in);
  14.         System.out.print("Input number type to be converted: ");
  15.         String answer = input.next();
  16.    
  17.         switch(answer.toLowerCase())
  18.         {
  19.         case "decimal":
  20.             System.out.println("You chose decimal! Please enter a decimal number: ");
  21.             int original = input.nextInt();
  22.             toBinary(original);
  23.             toOctal(original);
  24.             toHex(original);
  25.             break;
  26.            
  27.         case "binary":
  28.             System.out.println("You chose binary! Please eneter a binary number: ");
  29.             int original1 = input.nextInt();
  30.             int x2 = fromBinary(original1);
  31.             toOctal(x2);
  32.             toHex(x2);
  33.             break;
  34.        
  35.         case "octal":
  36.             System.out.println("You chose octal! Please enter an octal number: ");
  37.             int original2 = input.nextInt();
  38.             int x3 = fromOctal(original2);
  39.             toBinary(x3);
  40.             toHex(x3);
  41.             break;
  42.            
  43.         case "hex":
  44.             System.out.println("You chose hex! Please enter a hex number: ");
  45.             String original3 = input.next();
  46.             int x1 = Integer.parseInt(fromHex(original3));
  47.             toBinary(x1);
  48.             toOctal(x1);
  49.             break;
  50.            
  51.         case "stop":
  52.             System.out.println("Converter stopped");
  53.             loop = false;
  54.             break;
  55.         }
  56.         }
  57.     }
  58.  
  59.     public static String toBinary(int original)
  60.     {
  61.         int binary = original; 
  62.         String bResult = "";
  63.         int lastBinary = binary % 2;
  64.         String finalResult = "";
  65.        
  66.         while(binary >= 2)
  67.         {
  68.             binary = binary / 2;
  69.             bResult += Integer.toString(binary % 2);
  70.         }
  71.         System.out.print("Binary: ");
  72.         for(int x = bResult.length() - 1; x >= 0; x--)
  73.         {
  74.             finalResult += bResult.charAt(x);
  75.         }
  76.         finalResult += lastBinary;
  77.         System.out.println(finalResult);
  78.         return finalResult;
  79.     }
  80.    
  81.     public static String toOctal(int original)
  82.     {
  83.         int octal = original;
  84.         String oResult = "";
  85.         int lastOctal = octal % 8;
  86.         String finalResult = "";
  87.        
  88.         while(octal >= 2)
  89.         {
  90.             octal = octal / 8;
  91.             oResult += Integer.toString(octal % 8);
  92.         }
  93.         System.out.print("Octal: ");
  94.         for(int x = oResult.length() - 1; x >= 0; x--)
  95.         {
  96.             finalResult += oResult.charAt(x);
  97.         }
  98.         finalResult += lastOctal;
  99.         System.out.println(finalResult);
  100.         return finalResult;
  101.     }
  102.    
  103.     public static String toHex(int original)
  104.     {
  105.         int hex = original;
  106.         String hResult = "";
  107.         String finalResult = "";
  108.         if(original > 15 && original < 32)
  109.         {
  110.             switch(original)
  111.             {
  112.             case 16:
  113.                 hResult += ("01");
  114.                 break;
  115.             case 17:
  116.                 hResult += ("11");
  117.                 break;
  118.             case 18:
  119.                 hResult += ("21");
  120.                 break;
  121.             case 19:
  122.                 hResult += ("31");
  123.                 break;
  124.             case 20:
  125.                 hResult += ("41");
  126.                 break;
  127.             case 21:
  128.                 hResult += ("51");
  129.                 break;
  130.             case 22:
  131.                 hResult += ("61");
  132.                 break;
  133.             case 23:
  134.                 hResult += ("71");
  135.                 break;
  136.             case 24:
  137.                 hResult += ("81");
  138.                 break;
  139.             case 25:
  140.                 hResult += ("91");
  141.                 break;
  142.             case 26:
  143.                 hResult += ("A1");
  144.                 break;
  145.             case 27:
  146.                 hResult += ("B1");
  147.                 break;
  148.             case 28:
  149.                 hResult += ("C1");
  150.                 break;
  151.             case 29:
  152.                 hResult += ("D1");
  153.                 break;
  154.             case 30:
  155.                 hResult += ("E1");
  156.                 break;
  157.             case 31:
  158.                 hResult += ("F1");
  159.                 break;
  160.             }
  161.        
  162.         }
  163.         else
  164.         {
  165.            
  166.         while(hex >= 2)
  167.         {
  168.            
  169.             switch(hex % 16)
  170.             {
  171.             case 10:
  172.                 hResult += "A";
  173.                 break;
  174.             case 11:
  175.                 hResult += "B";
  176.                 break;
  177.             case 12:
  178.                 hResult += "C";
  179.                 break;
  180.             case 13:
  181.                 hResult += "D";
  182.                 break;
  183.             case 14:
  184.                 hResult += "E";
  185.                 break;
  186.             case 15:
  187.                 hResult += "F";
  188.                 break;
  189.            
  190.             default:
  191.                 hResult += Integer.toString(hex % 16);
  192.                 break;
  193.             }
  194.             hex = hex / 16;
  195.  
  196.         }
  197.         }
  198.         System.out.print("Hex: ");
  199.         for(int x = hResult.length() - 1; x >= 0; x--)
  200.         {
  201.             finalResult += hResult.charAt(x);
  202.         }  
  203.         System.out.println(finalResult);
  204.         return finalResult;
  205.     }
  206.  
  207.    
  208.     public static int fromOctal(int original)
  209.     {
  210.         int multiplied = 0;
  211.         int y = 0;
  212.        
  213.         String fromB = Integer.toString(original);
  214.         for(int x = fromB.length()-1; x >= 0; x--)
  215.         {
  216.             multiplied = multiplied + (Character.getNumericValue(fromB.charAt(x)) * (int)(Math.pow(8, y)));
  217.             y++;
  218.         }
  219.         System.out.println("Decimal: " + multiplied);
  220.         return multiplied;
  221.     }
  222.    
  223.     public static int fromBinary(int original)
  224.     {
  225.         int multiplied = 0;
  226.         int y = 0;
  227.        
  228.         String fromB = Integer.toString(original);
  229.         for(int x = fromB.length()-1; x >= 0; x--)
  230.         {
  231.             multiplied = multiplied + (Character.getNumericValue(fromB.charAt(x)) * (int)(Math.pow(2, y)));
  232.             y++;
  233.         }
  234.         System.out.println("Decimal: " + multiplied);
  235.         return multiplied;
  236.     }
  237.    
  238.     public static String fromHex(String original)
  239.     {
  240.         int multiplied = 0;
  241.         int y = 0;
  242.        
  243.         String fromB = original;
  244.         for(int x = fromB.length()-1; x >= 0; x--)
  245.         {
  246.             switch(fromB.charAt(x))
  247.             {
  248.                 case 'A':
  249.                     multiplied = multiplied + (10 * (int)(Math.pow(16, y)));
  250.                     break;
  251.                 case 'B':
  252.                     multiplied = multiplied + (11 * (int)(Math.pow(16, y)));
  253.                     break;
  254.                 case 'C':
  255.                     multiplied = multiplied + (12 * (int)(Math.pow(16, y)));
  256.                     break;
  257.                 case 'D':
  258.                     multiplied = multiplied + (13 * (int)(Math.pow(16, y)));
  259.                     break;
  260.                 case 'E':
  261.                     multiplied = multiplied + (14 * (int)(Math.pow(16, y)));
  262.                     break;
  263.                 case 'F':
  264.                     multiplied = multiplied + (15 * (int)(Math.pow(16, y)));
  265.                     break;
  266.                 default:
  267.                     multiplied = multiplied + (Character.getNumericValue(fromB.charAt(x)) * (int)(Math.pow(16, y)));
  268.                     break;
  269.             }
  270.            
  271.             y++;
  272.         }
  273.         System.out.println("Decimal: " + multiplied);
  274.         return Integer.toString(multiplied);
  275.     }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement