[Java]Convertitore Basi, Converte da base 2 a 16 e viceversa

« Older   Newer »
 
  Share  
.
  1. Jullian
        Like  
     
    .

    User deleted


    [Java]Convertitore Basi, Converte da base 2 a 16 e viceversa


    Questo semplice programma scritto in java vi permette di convertire un numero da base 2 a base 16 e viceversa. A voi indicare la base e inserire il valore, al programma operare le conversioni. Inoltre vi viene anche mostrato il corrispettivo valore decimale:

    CODICE
    import java.io.*;
    import java.lang.String;
    import java.lang.Math.*;
    import java.lang.reflect.Array;
    import java.lang.*;

    class Program
    {
          public static void main(String s[]){
                  BufferedReader inputStream;
                  String input;
                  char[] ch = null;
                  int[] numeroBaseN;
                  int numeroBase10;
                  int baseIn;
                  int baseOut;
                  int scelta = 0;
                  int resto = 0;
                 
                  inputStream = new BufferedReader(new InputStreamReader(System.in));
                 
                  System.out.println("Vuoi convertire da : n-0-Base2 a Base16n-1-Base16 a Base2n");
                 
                  try{
                          scelta = Integer.valueOf(inputStream.readLine()).intValue();
                  }catch(Exception e){
                  }
                 
                  //imposta le basi da input a output in base alla scelta fatta
                 
                  if (scelta == 0){
                          baseIn = 2;
                          baseOut = 16;
                          numeroBaseN = new int[4];
                  }else{
                          baseIn = 16;
                          baseOut = 2;
                          numeroBaseN = new int[16];
                  }
                 
                  //legge il valore da convertire
                 
                  numeroBase10 = 0;
                  try{
                          System.out.printf("Inserisci il valore in base %d : ", baseIn);
                          input = inputStream.readLine();                
                          ch = input.toCharArray();
                  }catch(Exception e){
                  }
                 
                  //Conversione da Base Input a Base 10
                 
                  for (int i = 0, cifra = 0; i < Array.getLength(ch); i++){
                          if (ch[Array.getLength(ch) - i - 1] >= 'A')
                                  cifra = ch[Array.getLength(ch) - i - 1] - 'A' + 10;
                          else
                                  cifra = ch[Array.getLength(ch) - i - 1] - '0';
                          numeroBase10 = numeroBase10 + (cifra * (int)Math.pow(baseIn, i));
                  }
                 
                  System.out.printf("RIS = %dn",numeroBase10);
                 
                  //Conversione da Base 10 a Base Output
                 
                  int i = 0;
                 
                  while(numeroBase10 > 0){
                          numeroBaseN[Array.getLength(numeroBaseN) - i - 1] = numeroBase10 % baseOut;
                          numeroBase10 = numeroBase10 / baseOut;
                          i++;
                  }
                 
                  //Output
                 
                  for(int k = 0; k < Array.getLength(numeroBaseN); k++)
                          if (numeroBaseN[k] >= 10)
                                  System.out.printf("%c", (numeroBaseN[k] % 10) + 'A');
                          else
                                  System.out.printf("%c", (numeroBaseN[k] + '0'));
                  System.out.printf("n");
                 
          }
    }

     
    .
0 replies since 17/5/2010, 10:19   113 views
  Share  
.