Monday 10 July 2017

Java Code for base conversion.

Problem:

Design an algorithm to convert a number to be in any base up to 36.

Solution:

package com.myprograms;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BaseConversion {

static String n;
static int outputBase;
static int inputBase;
List<Character> digits = new ArrayList<Character>();
static char[] result;
public static void main(String[] args) {
BaseConversion baseConversion = new BaseConversion();
baseConversion.getTheNumber();
result = baseConversion.baseConversion(n.toCharArray(), inputBase, outputBase);
baseConversion.printTheResult("the converted number is: ");

}

public void getTheNumber(){
Scanner s = new Scanner(System.in);
System.out.println("what is your input base??");
inputBase = s.nextInt();
System.out.println("what is your expected output base??");
outputBase = s.nextInt();
System.out.println("enter the number");
n = s.next();
s.close();
}

public void printTheResult(String message){
System.out.print(message + " ");
for(Character c: result){
System.out.print(c);
}
}

private char intToChar(int i)
{
if(i >= 0 && i <= 9)
return (char)(i + 48);
else if(i >= 10 && i <= 35)
return (char)(i + 55);
else
return (char)(i);

}

private int charToInt(char c)
{
if(c >= '0' && c <= '9')
return (int)(c - 48);
else if(c >= 'A' && c <= 'Z')
return (int)(c - 55);
else if(c >= 'a' && c <= 'z')
return (int)(c - 87);
else
return 0;
}

char[] baseConversion(char num[], int ibase, int fbase)
{
int n=toDecimal(num, ibase);
char[] arr;
arr = decimalToBase(n, fbase);
return arr;
}

char[] decimalToBase(int num, int base)
{
if(base>36 || base<2)
return new char['0'];
int rem, i=0;
char[] arr = new char[100];
char[] arr2 = new char[100];
while(num != 0)
{
rem = num%base;
arr[i]=intToChar(rem);
num = num/base;
i++;
}
int j;
for(j=0; j<i; j++)//Reverse the arr[]
arr2[j] = arr[i-j-1];
return arr2;
}

int toDecimal(char num[], int base)
{
if(base > 36 || base < 2)
return -1;
int sum = 0;
for(int k = 0; k<num.length; k++){
sum = sum*base + charToInt(num[k]);
}
return sum;
}
}


Output:

what is your input base??
10
what is your expected output base??
8
enter the number
93
the converted number is:  135


what is your input base??
10
what is your expected output base??
2
enter the number
93
the converted number is:  1011101

what is your input base??
10
what is your expected output base??
16
enter the number
93
the converted number is:  5D


what is your input base??
2
what is your expected output base??
10
enter the number
1011101
the converted number is:  93


what is your input base??
2
what is your expected output base??
8
enter the number
1011101
the converted number is:  135

what is your input base??
2
what is your expected output base??
16
enter the number
1011101
the converted number is:  5D




No comments:

Post a Comment

Program for primality test in JAVA

Problem: ============= Program for primality test in JAVA What is primality test? A primality test is an algorithm for determining wh...