2

Can't find a good example anywhere. How do I convert a Base 20 number to decimal? I'm working with vigesimal numbers. I know how to convert from Vigesimal to decimal, that part is easy. But I'm trying to convert from Vigesimal back to decimal (integer) and I'm not sure how to do this. Anyone have any good examples? I am using c++ by the way.

For example, I'm trying to figure out an algorithm that will Convert the Vigesimal number of 12 to its decimal equivalent which is 22 (integer). Or something like:

30 (integer) = 1A (vigisemal)

Thanks in advance,

  • You do realize that your statement "I know how to convert from Vigesimal to decimal, that part is easy. But I'm trying to convert from Vigesimal back to decimal " might be confusing. Based on the rest of the context I think I know what you mean - but you do contradict yourself a bit. Maybe want to edit the question? – Floris Apr 13 '14 at 04:28

3 Answers3

1

It's really just basic math. Let's assume that the number you have is in a string. Then (pseudo code)

number = "AF8";
base = 20;
result = 0;
for each c in number:
  temp = numberValueOf(char)
  result = base * result + temp

print "converting ", number, " results in ", result

Here, the numberValueOf function converts the characters 0 to 9 to their equivalent value, and A to J become 10 through 19.

Floris
  • 45,857
  • 6
  • 70
  • 122
1

This does it both ways (Vigesimal to Decimal and Decimal to Vigesimal)

#!/usr/bin/env python

 def convert(vigisemal):
    i = int(vigisemal, 20)
    return i


 def tovigisemal(decimalstring):
     dec = int(decimalstring)
    x = (dec % 20)
    digits = "0123456789ABCDEFGHIJ"
    rest = dec / 20
    if (rest == 0):
        return digits[x]
    return tovigisemal(rest) + digits[x]


if __name__ == '__main__':
    print(convert("H1"))
    print(tovigisemal("341"))

based on Lynch's Answer

Community
  • 1
  • 1
Ansel Zandegran
  • 636
  • 8
  • 18
0

In any positional number system, any number (even fractional numbers) is represented as sum(di * basei), where di is the digit in the i-th place (counted from right to left) with d0 being the digit in the first position left of the decimal point. For ease of its evaluation evaluation, the sum could be "folded" using Horner's rule:

(((d[n]*base + d[n-1])*base + d[n-2])*base + d[n-3])*base + ...

The good thing about this one is that one could start from the most significant digit and work his was to the right which follows the way digits are ordered in memory or file strings.

string number = "1A";
int decimal = 0;

for (string::const_iterator it = number.begin(); it != number.end(); it++)
   decimal = decimal * base + to_int(*it);

to_int() should take care of converting a character to its decimal value, e.g.

int to_int (char d) {
  if ('0' <= d && d <= '9')
    return d - '0';
  else if ('A' <= d && d <= 'J')
    return 10 + (d - 'A');
  else
    throw some error
}

C++ provides the convenient stoi() function which does the math for you:

#include <string>
#include <iostream>

using namespace std;

int main (void) {
  string number = "1A";

  cout << number << " in decimal is " << stoi(number, 0, 20) << endl;
}

Compile and run:

$ clang++ -o visc visc.cc && ./visc
1A in decimal is 30

In C one could use strtol() with the base set to 20. But it is supposed to be used in input tokenizers and modifies its input argument so care should be taken.

In Python the string module provides atoi() with selectable base:

>>> from string import atoi
>>> atoi("1A", 20)
30

Other languages provide similar conversion functions too, mostly found in their string processing libraries.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186