Go4Expert (http://www.go4expert.com/)
-   Engineering Concepts (http://www.go4expert.com/articles/engineering-concepts-tutorials/)
-   -   Converting Between Numeric Bases (http://www.go4expert.com/articles/converting-numeric-bases-t3099/)

 Febian 21Feb2007 21:38

Converting Between Numeric Bases

In mathematical numeral systems, the base or radix is usually the number of various unique digits, including zero, that a positional numeral system uses to represent numbers. For example, the decimal system, the most common system in use today, uses base ten, hence the maximum number a single digit will ever reach is 9, after which it is necessary to add another digit to achieve a higher number.

The highest symbol of a positional numeral system usually has the value one less than the value of the radix of that numeral system. The standard positional numeral systems differ from one another only in the radix they use. The radix itself is almost always expressed in decimal notation. The radix is an integer that is greater than 1, since a radix of zero would not have any digits, and a radix of 1 would only have the zero digit.

### Converting Between Bases

You are given the number 13 in base ten; this means 1 ten and 3 ones.
You want to write it in base five, as some number of fives and some number of ones. (If the number were big enough, you might also need 25's, 125's, and so on, just as in base ten you might need hundreds or thousands.)

So here is 13:

Code:

```    ___10___  1 1 1     /        \     oooooooooo o o o     \________/ \___/         1      3         ten    ones```
Let's group it by 5's rather than 10's:

Code:

```    _5_  _5_  1 1 1     /  \ /  \     ooooo ooooo o o o     \_________/ \___/         2        3       fives    ones```
So we have 2 fives and 3 ones; in base five, we write this as 23 (base 5).

Let's try a bigger number: 68 (base ten). We can group it in fives by just dividing by 5, rather than drawing pictures:
68 / 5 = 13 remainder 3
So we have 13 fives and 3 ones.

But we're not done, because in base five we can't have any digits greater than 4 (just as in base ten we have no digits greater than 9). So now we have to group our 13 fives into groups of five fives.

Divide again:
13 / 5 = 2 remainder 3
This tells us that 13 is 2 fives and 3 ones; so 13 FIVES is 2 twenty-fives and 3 fives. Our number is then
68 (base ten) = 2 (25's) + 3 (5's) + 3 (1's)
which we write as 233.

Code:

```    _25  _25  _5_  _5_  _5_  1 1 1     /  \ /  \ /  \ /  \ /  \     ooooo ooooo ooooo ooooo ooooo o o o     ooooo ooooo     ooooo ooooo     ooooo ooooo     ooooo ooooo     \_________/ \_______________/ \___/         2              3          3     twenty-fives      fives        ones```
Check it out:
2x25 + 3x5 + 3x1 = 50 + 15 + 3 = 68
Converting Between Bases Programatically (PHP)

Code: PHP

`define("HEX",16);define("BINARY",2);define("OCT",8);define("BASE10",10);define("DECIMAL", 10);function convertBase(\$number, \$fromBase = 10, \$toBase = 2) {      if(\$toBase > 36 || \$toBase < 2)   //check base validity        return "Invalid originating base.";    if(\$fromBase > 36 || \$fromBase < 2)        return "Invalid destination base.";    @list(\$number, \$decimal) = explode(".",\$number);    for(\$i = 0; \$i < strlen(\$number); \$i++) //convert to base 10    {              \$digit = substr(\$number, \$i, 1);        if(eregi("[a-z]",\$digit))         {            \$x = ord(\$digit) - 65 + 10;            if(\$x > \$fromBase)            \$x -= 32;            \$digit = \$x;        }        @\$base10 += \$digit * (pow(\$fromBase, strlen(\$number) - \$i - 1));    }    \$number = \$base10;    if(\$toBase == 10)    return \$number;    \$q = \$number;    while(\$q != 0) //convert base 10 equivalent to specified base    {              \$r = \$q % \$toBase;        \$q = floor(\$q / \$toBase);        if(\$r > 9)          \$r = chr((\$r - 9) + 64);        @\$baseres = "\$r" . "\$baseres";    }    return \$baseres;}`

Example usage:
Code: PHP

`print convertBase(15,DECIMAL,HEX); // when you are converting between standard basesprint convertBase(15,7,5); // any base you would like to give  `

 pobi 30Aug2008 17:21

very inspiring.

Quote:
 In mathematical numeral systems, the base or radix is usually the number of various unique digits, including zero, that a positional numeral system uses to represent numbers. For example, the decimal system, the most common system in use today, uses base ten, hence the maximum number a single digit will ever reach is 9, after which it is necessary to add another digit to achieve a higher number.
i remember my schooldays whenever i read statements like this sigh. i also remember my professors. :embarasse

 shipra123 1Dec2009 18:10

Re: Converting Between Numeric Bases

Seriously Pobi,I completely agree with you. The moment I read this post, I went nostalgic with the school and college day.

 SEO_services 19Jan2011 14:02

Re: Converting Between Numeric Bases

Explained very well and good links to it.

 All times are GMT +5.5. The time now is 09:05.