-4

Possible Duplicate:
Efficient algorithm for conversion between numeral system

Given an integer, write a program that converts the given number to a number (in base 10). Hint - The given number could be in any base, but the base is unknown.

Community
  • 1
  • 1
java_geek
  • 17,585
  • 30
  • 91
  • 113
  • 1
    Should this be tagged homework? – Thomas Ahle May 28 '10 at 06:29
  • 3
    How is that even possible? What base is "4" ? It could be base 5, base 12, base 123 - you have now way of knowing! – Dean Harding May 28 '10 at 06:30
  • 2
    Maybe it should read: convert a number in base 10 to a number in any other base? – filip-fku May 28 '10 at 06:37
  • 4
    @codeka - 4 in all of those bases is still 4. A more interesting question would be, what base is "10"? :P – detly May 28 '10 at 06:50
  • @detly: ha, yes you're right but hopefully you get my point... :) – Dean Harding May 28 '10 at 07:01
  • Various other dupes too, all phrased more clearly than this: http://stackoverflow.com/questions/923771/quickest-way-to-convert-a-base-10-number-to-any-base-in-net, http://stackoverflow.com/questions/2894325/an-algorithm-for-converting-a-base-10-number-to-a-base-n-number, etc – bstpierre Sep 28 '12 at 02:07

7 Answers7

11

That can't be done; without knowing the source base the number is ambiguous. 10 in base n translates to n in base 10; there are infinite possibilities

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
9

I'm assuming by 'unknown' you mean the algorithm needs to be able to handle any base? Otherwise it's just plain impossible.

So you're basically asking for function convert(number, base) = base10Number?

count = 0
total = 0
for each digit in number, from least significant to most significant
  total = total + digit * base^count
  count = count + 1

e.g. convert(355,8)

  • first loop: total = 0 + 5 * 8^0 = 5
  • second loop: total = 5 + 5 * 8^1 = 45
  • third loop: total = 45 + 3 * 8^2 = 237

Result = 237

Flynn1179
  • 11,925
  • 6
  • 38
  • 74
2

You can make a simple algorithm changes n from base c to base b, where:

  • n is a list of the digits making up the number.
  • c is the initial base.
  • b is the desired base.

Each digit may contain more than one digit. Below is an implementation of Wallar's algorithm in Python.

from math import *
def baseExpansion(n,c,b):
    j = 0
    base10 = sum([pow(c,len(n)-k-1)*n[k] for k in range(0,len(n))])
    while floor(base10/pow(b,j)) != 0: j = j+1
    return [floor(base10/pow(b,j-p)) % b for p in range(1,j+1)]
Glastis
  • 163
  • 4
  • 15
Alex
  • 37
  • 2
1

It is easy to do, once you've got the base.

You can get a lower bound for the base, by finding the highest digit. Like in the number 175234 the base must be at least 8. However you can never find an upper bound: The number could be any base from 8 to infinity.

Instead you can print out the number it would be, given the first base was e.g. 8, 9 or 10. Then the user can decide what he/she thinks.

Thomas Ahle
  • 30,774
  • 21
  • 92
  • 114
1

Here is a small java example with three methods.

public class TestNumberBase {
    public static void main(String[] args) {
        System.out.println(converNumberTObase(100000, 2, 16));
    }

    public static int converNumberTObase(int inNum, int inBase, int outBase) {
        return convertDecimalToOtherBase(convertDecimalEquivalent(inNum, inBase), outBase);
    }

    public static int convertDecimalEquivalent(int number, int inBase) {
        int outNumber = 0;
        int _base = inBase;

        while (number > 0) {
            int digit = number % 10;
            number = number / 10;

            outNumber = outNumber + (inBase / _base) * digit;
            inBase = inBase*_base;
        }
        return outNumber;
    }

    public static int convertDecimalToOtherBase(int number, int outBase) {
        int outNumber = 0;
        int _base = 10, base =10;

        while (number > 0) {
            int digit = number % outBase;
            number = number / outBase;

            outNumber = outNumber + (base / _base) * digit;
            base = base*_base;
        }
        return outNumber;
    }
}
Glastis
  • 163
  • 4
  • 15
Neel Salpe
  • 1,287
  • 3
  • 15
  • 26
0

This is wrong question because consider that number 7 it may be in octal system , hexadecimal system .It is not possible to decide .We must know input numbers base . We can write method like this

public int convertToBase(int inNumber , int inBase , int outBase){

// blah blah

return convertedNumber; }

Neel Salpe
  • 1,287
  • 3
  • 15
  • 26
-1

The problem statement states that the base of the given number is unknown. Thus to proceed one must need to assume a base for the number. It is practically safe to assume that the digit with the maximum value in the number denotes the maximum that can be accounted in the unknown base. This number, for example if stated as, 254, it can be assumed that the number system consists of digits 0, 1, 2, 3, 4, 5 - or base 6.

if(!(((ascii >= '0') && (ascii <= '9')) || ((ascii >= 'A') && (ascii <= 'Z')))) {
   printf("Illegal number, can have only digits (0-9) and letters (A-Z)");

Hope this helps.

manav m-n
  • 11,136
  • 23
  • 74
  • 97