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.
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.
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
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)
Result = 237
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)]
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.
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;
}
}
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; }
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.