Advertisement
Guill

updt 26/08/2013

Aug 26th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.27 KB | None | 0 0
  1. package javaapplication3;
  2.  
  3. public class JavaApplication3 {
  4.  
  5.     public static void main(String[] args) {
  6.    //     dectobin(args,2036);
  7.    //     bintodec(args, "1111");
  8.    //     octtodec(args, "10021");
  9.    //     dectooct(args, 4113);
  10.    //     dectohex(args, 9889);
  11.         hextodec(args, "0");    
  12.     }
  13.    
  14.     public static void JavaApplication3() {
  15.  
  16.     }
  17.    
  18.    
  19.     static void hextodec(String[] args, String a){
  20.         int[] tab = new int[255];
  21.         int i = 0;
  22.         while(i <= a.length() - 1){
  23.             tab[i] = (int) Math.pow(16,i);
  24.             p(tab[i]);
  25.             i = i + 1;
  26.         }
  27.         int r = 0;
  28.         i = 0;
  29.         int l = a.length() - 1;
  30.         while(i <= a.length() - 1){
  31.             r = r + (nehex(a.charAt(i)) * tab[l]);
  32.             l = l - 1;
  33.             i = i + 1;
  34.             p("\n" + r);
  35.         }
  36.         p("\n" + r);
  37.    
  38.     }
  39.        
  40.     static void octtodec(String[] args, String a){
  41.         if(a.contains("8") || a.contains("9")){
  42.             return;
  43.         }
  44.         int[] tab = new int[255];
  45.         int i = 0;
  46.         while(i <= a.length() - 1){
  47.             tab[i] = (int) Math.pow(8,i);
  48.             p(tab[i]);
  49.             i = i + 1;
  50.         }
  51.         int r = 0;
  52.         i = 0;
  53.         int l = a.length() - 1;
  54.         while(i <= a.length() - 1){
  55.             r = r + (Integer.valueOf(String.valueOf(a.charAt(i))) * tab[l]);
  56.             l = l - 1;
  57.             i = i + 1;
  58.         }
  59.         p("\n" + r);
  60.       /*  while(i > 0){
  61.             i = i - 1;
  62.             if(a >= tab[i]){
  63.                 r =  r + "1";
  64.                 a = a - tab[i];
  65.             }
  66.             else
  67.             {
  68.                 r = r + "0";
  69.             }
  70.         }
  71.         p("\n" + r); */
  72.     }
  73.    
  74.     static String hexen(Integer n){
  75.         String a = Integer.toString(n);
  76.         if(n < 10){
  77.             return a;
  78.         }
  79.         switch(n){
  80.             case 10: return "A";
  81.             case 11: return "B";
  82.             case 12: return "C";
  83.             case 13: return "D";
  84.             case 14: return "E";
  85.             case 15: return "F";
  86.         }      
  87.         return "";
  88.     }      
  89.    
  90.     static Integer nehex(Character n){
  91.         switch(n){
  92.             case 'A': return 10;
  93.             case 'B': return 11;
  94.             case 'C': return 12;
  95.             case 'D': return 13;
  96.             case 'E': return 14;
  97.             case 'F': return 15;  
  98.             default: break;    
  99.         }    
  100.         int a = Integer.getInteger(String.valueOf(n));
  101.         if(a < 10){
  102.             return a;
  103.         }      
  104.         return 0;
  105.     }
  106.    
  107.    
  108.    
  109.     static void dectohex(String[] args, Integer a){
  110.         String r = "";
  111.         while(a > 0){
  112.             r = hexen(a % 16) + r;
  113.             a = a / 16;
  114.             p(a + "\n");
  115.         }
  116.         p("\n" + r);
  117.     }
  118.    
  119.     static void dectooct(String[] args, Integer a){
  120.         String r = "";
  121.         while(a > 0){
  122.             r = Integer.toString(a % 8) + r;
  123.             a = a / 8;
  124.             p(a + "\n");
  125.         }
  126.         p("\n" + r);
  127.     }
  128.    
  129.    
  130.    
  131.    
  132.    
  133.     static void dectobin(String[] args, Integer a){
  134.         int[] tab = new int[255];
  135.         int i = 0;
  136.         while(Math.pow(2,i) <= a){
  137.             tab[i] = (int) Math.pow(2,i);
  138.             p(tab[i]);
  139.             i = i + 1;
  140.         }
  141.         String r = "";
  142.         while(i > 0){
  143.             i = i - 1;
  144.             if(a >= tab[i]){
  145.                 r =  r + "1";
  146.                 a = a - tab[i];
  147.             }
  148.             else
  149.             {
  150.                 r = r + "0";
  151.             }
  152.         }
  153.         p("\n" + r);
  154.     }
  155.      
  156.     static void bintodec(String[] args, String a){
  157.         int[] tab = new int[255];
  158.         int i = a.length() - 1;        
  159.         while(i >= 0){
  160.             tab[i] = (int) Math.pow(2,i);
  161.             p(tab[i]);
  162.             i = i - 1;
  163.         }
  164.         i = a.length() - 1;
  165.         int r = 0;
  166.         int l = 0;
  167.         while(i >= 0){
  168.             r = r + (Integer.valueOf(String.valueOf(a.charAt(i))) * tab[l]);                  
  169.             l = l + 1;
  170.             i = i - 1;
  171.         }
  172.         p("\n" + Integer.toString(r));
  173.     }
  174.      
  175.     static void p(Object stri){
  176.         System.out.print(stri);
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement