Number conversion Show your work Base 10 to 2 8 16 18073 Ba
Number conversion - Show your work
Base 10 to 2, 8, 16: 18073
Base 8 to 2, 10, 16: 76104
Base 16 to 2, 8, 10: D10CA3B8
Base 2 to 8, 16: 1010 1100 1111 1101 0111 1001
This is for Java
Solution
//BaseConverter.java
import java.lang.String;
import java.util.Scanner;
public class BaseConverter
{
//This Function convert string s in fBase to toBase
static String base2base(String s, int fromBase, int toBase)
{
int number = fBaseToDecimalConversion(s, fromBase);
return DecimalToBaseCoversion(number, toBase);
}
//This function convert a decimal value into other base
static String DecimalToBaseCoversion(int decimalValue, int toBase)
{
String result = \"\";
int reminder;
// Convert input decimalValueber is given toBase by repeatedly
// dividing it by toBase and taking reminderainder
while (decimalValue > 0)
{
reminder = decimalValue % toBase;
if (toBase == 16)
{
if (reminder == 10)
result=result+ \'A\';
else if (reminder == 11)
result=result+ \'B\';
else if (reminder == 12)
result=result+ \'C\';
else if (reminder == 13)
result=result+ \'D\';
else if (reminder == 14)
result=result+ \'E\';
else if (reminder == 15)
result=result+ \'F\';
else
result=result+ reminder;
} else
result=result+ reminder;
decimalValue /= toBase;
}
// Reverse the resultult
return new StringBuffer(result).reverse().toString();
}
//this function convert a string of numbers (which is compatible to fBase) in Decimalvalue
static int fBaseToDecimalConversion(String s, int fBase)
{
if (fBase < 2 || (fBase > 10 && fBase != 16))
return -1;
int value = 0;
int power = 1;
for (int i = s.length() - 1; i >= 0; i--) {
int singleDigit = digitToValue(s.charAt(i));
if (singleDigit < 0 || singleDigit >= fBase)
return -1;
// Decimal equivalueent is str[len-1]*1 +
// str[len-1]*fBase + str[len-1]*(fBase^2) + ...
value += singleDigit * power;
power = power * fBase;
}
return value;
}
//This Function convert a digit value into character for base System
static int digitToValue(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(\"Base 10 value: 18073\" );
System.out.println(\"to base 2: \" + base2base(\"18073\",10,2));
System.out.println(\"to base 8: \" + base2base(\"18073\",10,8));
System.out.println(\"to base 16: \" + base2base(\"18073\",10,16));
System.out.println();
System.out.println(\"Base 8 value: 76104\" );
System.out.println(\"to base 2: \" + base2base(\"76104\",8,2));
System.out.println(\"to base 10: \" + base2base(\"76104\",8,10));
System.out.println(\"to base 16: \" + base2base(\"76104\",8,16));
System.out.println();
System.out.println(\"Base 16 value: D10CA3B8\" );
System.out.println(\"to base 2: \" + base2base(\"D10CA3B8\",16,2));
System.out.println(\"to base 8: \" + base2base(\"D10CA3B8\",16,8));
System.out.println(\"to base 10: \" + base2base(\"D10CA3B8\",16,10));
System.out.println();
System.out.println(\"Base 2 value: 1010 1100 1111 1101 0111 1001\" );
System.out.println(\"to base 8: \" + base2base(\"101011001111110101111001\",2,8));
System.out.println(\"to base 16: \" + base2base(\"101011001111110101111001\",2,16));
System.out.println();
}
}
/*
output:
Base 10 value: 18073
to base 2: 100011010011001
to base 8: 43231
to base 16: 4699
Base 8 value: 76104
to base 2: 111110001000100
to base 10: 31812
to base 16: 7C44
Base 16 value: D10CA3B8
to base 2: 11010001000011001010001110111000
to base 8: 32103121670
to base 10: 3507266488
Base 2 value: 1010 1100 1111 1101 0111 1001
to base 8: 53176571
to base 16: ACFD79
*/


