Advertisement
Guill

going last 26/08

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