Convert from one base to another in Java
You could do
return Integer.toString(Integer.parseInt(number, base1), base2);
So with your function signature, in Java:
public String convertFromBaseToBase(String str, int fromBase, int toBase) {
return Integer.toString(Integer.parseInt(str, fromBase), toBase);
}
public class BaseToBaseConv {
static String baseToBase(String num, int base1, int base2) {
int no = convFrmBaseToDeci(num, base1);
return convFrmDecToBase(no, base2);
}
static String convFrmDecToBase(int num, int base) {
String res = "";
int rem;
// Convert input number is given base by repeatedly
// dividing it by base and taking remainder
while (num > 0) {
rem = num % base;
if (base == 16) {
if (rem == 10)
res += 'A';
else if (rem == 11)
res += 'B';
else if (rem == 12)
res += 'C';
else if (rem == 13)
res += 'D';
else if (rem == 14)
res += 'E';
else if (rem == 15)
res += 'F';
else
res += rem;
} else
res += rem;
num /= base;
}
// Reverse the result
return new StringBuffer(res).reverse().toString();
}
static int convFrmBaseToDeci(String num, int base) {
if (base < 2 || (base > 10 && base != 16))
return -1;
int val = 0;
int power = 1;
for (int i = num.length() - 1; i >= 0; i--) {
int digit = digitToVal(num.charAt(i));
if (digit < 0 || digit >= base)
return -1;
// Decimal equivalent is str[len-1]*1 +
// str[len-1]*base + str[len-1]*(base^2) + ...
val += digit * power;
power = power * base;
}
return val;
}
static int digitToVal(char c) {
if (c >= '0' && c <= '9')
return (int) c - '0';
else
return (int) c - 'A' + 10;
}
public static void main(String [] args) {
System.out.println(baseToBase("12345", 10, 2));
System.out.println(baseToBase("11000000111001", 2, 10));
System.out.println(baseToBase("ABC11", 16, 2));
System.out.println(baseToBase("10101011110000010001", 2, 16));
System.out.println(baseToBase("12322", 8, 16));
}
}
The two-argument versions of Integer.parseInt
or Long.parseLong
will do this if you can be sure the number in question is within the range of int
or long
respectively. If you can't guarantee this, use java.math.BigInteger
:
BigInteger bi = new BigInteger(number, base1);
return bi.toString(base2);
This can handle arbitrarily-large integers, for example
System.out.println(
new BigInteger("12345678901234567890123456789", 10).toString(16));
// prints 27e41b3246bec9b16e398115 - too big to represent as a long
I believe this will work:
long x = 10;
int baseToConvertTo = 9;
System.out.println(Long.toString(x, baseToConvertTo));
Output: 11
As others have displayed,Integer.parseInt() can do this for you. However, if you're trying to build a converter yourself, the following will work to simply convert a numeric value to the desired radix. Note, for radix above base 10 you have to consider the alpha chars ... I.E. 11-A, 12-B, etc...
public class NumberUtil {
/**
* This example is convoluted as in reality it just uses 'toString' to convert the number...
* However, it displays the logic needed to make the conversion...
*
* To convert a number to a new radix, recursively return the remainder of the number
* divided by the radix for each operation until zero. Then return the concatenated value in reverse.
*
* Example convert 9658 to base 2
*
* 9658 / 2 = 4829 R 0
* 4829 / 2 = 2414 R 1
* 2414 / 2 = 1207 R 0
* 1207 / 2 = 603 R 1
* 603 / 2 = 301 R 1
* 301 / 2 = 150 R 1
* 150 / 2 = 75 R 0
* 75 / 2 = 37 R 1
* 37 / 2 = 18 R 1
* 18 / 2 = 9 R 0
* 9 / 2 = 4 R 1
* 4 / 2 = 2 R 0
* 2 / 2 = 1 R 0
* 1 / 2 = 0 R 1
*
* Answer :: 10010110111010
*
* @param number :: Integer number to convert.
* @param radix :: Radix to convert to.
* @return :: BigInteger of the number converted to the desired radix.
*/
static BigInteger convertBase( int number, int radix ) {
List<Integer> remainder = new ArrayList<>();
int count = 0;
String result = "";
while( number != 0 ) {
remainder.add( count, number % radix != 0 ? number % radix : 0 );
number /= radix;
try {
result += remainder.get( count );
} catch( NumberFormatException e ) {
e.printStackTrace();
}
}
return new BigInteger( new StringBuffer( result ).reverse().toString() );
}
public static void main( String[] args ) {
System.out.println( convertBase( 9658, 2 ) );
}
}