Advertisement
Omar_Natour

Digital Logic base converter

Sep 22nd, 2021
1,154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. //Omar Natour
  2.  
  3. package baseConverter;
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class BaseConverter {
  8.    
  9.     static int[] reverse(int original[],int k) {
  10.         int[] finalArray = new int[k];
  11.         k=k-1;
  12.  
  13.         for(int i=0; i<=k; i++) {
  14.             finalArray[i] = original[k-i];
  15.         }
  16.         return finalArray;
  17.     }
  18.    
  19.     public static void main(String[] args) {
  20.        
  21.         Scanner input = new Scanner(System.in);
  22.        
  23.         int x;
  24.         int z;
  25.         int j = 0;
  26.        
  27.         System.out.print("Please enter a base10 number to have it converted to base8:");
  28.         x = input.nextInt();
  29.         input.close();
  30.        
  31.         System.out.print(x + " in base10 converted to base8 is ");
  32.        
  33.         String llength = Integer.toString(x);
  34.         z = llength.length();
  35.         z=z+1;
  36.        
  37.         int[] remainderArray = new int[z];
  38.        
  39.         while(x>0) {
  40.             remainderArray[j] = x%8;
  41.             x=x/8;
  42.             j++;
  43.         }
  44.        
  45.         int[] finalArr = new int[z];
  46.         finalArr = reverse(remainderArray,remainderArray.length);
  47.        
  48.         for(int i=0;i<z;i++) {
  49.             System.out.print(finalArr[i]);
  50.         }  
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement