2

This is not a homework problem, I am too old to get home works :)

So, ideally I am trying to convert a number in a given base to another given base.

Can someone please share the logic, then probably I can write the code myself. Not able to find anything online surprisingly.

2

4 Answers 4

2

The answer depends on whether or not you can use a primitive, such as an int or long for your representation.

If you can, the algorithm is reasonably simple: convert the number in base X to a primitive representation, then convert that representation to base Y.

To convert a number to primitive, use this algorithm:

  • Make a running total res, and set it to zero
  • Go through the string representing the number number in base X left-to-right
  • Convert each "digit" (which may be represented by a letter) to its numeric value
  • Multiply running total by X, then add the numeric value of the digit to it

To convert back, use this algorithm:

  • Make a string builder
  • Remove the value of the last digit by obtaining digit = num % Y
  • Convert the digit value to digit character (it may be a letter)
  • Append the digit character to the string builder
  • Drop the last digit from representation by using num /= Y
  • Continue while num is not zero
  • Reverse the string in the string builder

If your number is too big for a primitive, such as int or long, you need to build a class for holding numbers greater than primitives. I would recommend using BigInteger initially, and then replacing it with your own implementation.

2
  • Thanks for replying. I did not get it completely. Do you mean first converting the number to decimal ? If no then can I please request an example. If yes then Is there a way to directly convert from one base to other without actually computing the number (which is equivalent to changing it to decimal, if I am not wrong) ?
    – Walt
    Jan 24, 2015 at 10:31
  • 1
    @Walt This is not a conversion to decimal, this is a conversion to internal representation built into Java (i.e. whatever is supported by the hardware, which is pretty much always a binary representation). You can go from one representation to another directly if you build code for dividing string representations of numbers in base X, and addition/multiplication in base Y. This is a lot trickier, though. In some special cases, like base-4 to base-32, you can take shortcuts, but in general it's hard. Jan 24, 2015 at 10:45
0

You could try this:

String input = /* your input */;
int inputBase = /* your input base */;
int outputBase = /* wished output base */;

int inputInt = Integer.valueOf(input, inputBase);
String output = Integer.toString(inputInt, outputBase);
1
  • Maybe the person who's downvoted my answer could be kind enough to share the reason for that? Jan 24, 2015 at 10:28
0

Is the number a scalar or a fraction?

Extra work is required to convert the fraction part.

And it is not easy to cheat, unlike BigInteger.toString(), BigDecimal.toString() does not take a radix.

0

General algorithm is :

Say you get a number in base x. It means Σ ai xi

  • first compute the number as a long or a BigInteger if too huge simply by iteratively computing above formula : n = a 0 + x * a1 + ...
  • then decompose n to base y : n0 = n, b0 = n0 % y

    then iteratively : ni = ni-1 - bi-1 / y , bi = ni % y

And you get base y representation : Σ bi yi

2
  • Is there a way to directly convert from one base to other without actually computing the number (which is equivalent to changing it to decimal, if I am not wrong)
    – Walt
    Jan 24, 2015 at 10:29
  • @Walt : it is equivalent to changing it to binary if using int or long type and to decimal if using big decimal. AFAIK, you can only avoid computing the number if the 2 base have exactly same divisors. Example 8 -> 16 : 4 base8 digits exactly correspond to 3 base16 digits. But in the general case, it is simpler to compute the full number. Jan 24, 2015 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.