# Assembly Language Programming

## February 3, 2011

### Javascript – Binary To Decimal Conversion

The Javascript/Html code below converts an arbitrarily large string containing a binary, hexadecimal or octal number to a decimal value. Theoretically, the size of the numbers that this code can handle is unlimited. The algorithm that the code uses to do the conversion is the Remainder Method.

## The Remainder Method

To convert some value stored in memory to any number system of radix R, just calculate:

Q(i-1)/R  –>  Q(i) and r(i)

– until Q(i) becomes zero for some i=n. Each remainder r(i) gives a digit in the output value, as follows:

Q(R) = r(n)r(n-1) … r(3)r(2)r(1)r(0)

In other words, dividing Q by R will give a remainder r in the range [0, R-1], and this will just be the right most digit in the base R representation of Q.

A simple example. Consider the value 1234, stored in memory as a binary value. A decimal representation of this value is wanted (R=10):

 ```Q=1234, R=10 Q(0) = 1234/10 = 123, and r(0) = 4 Q(1) = 123/10 = 12, and r(1) = 3 Q(2) = 12/10 = 1, and r(2) = 2 Q(3) = 1/10 = 0, and r(3) = 1 Q(R=10) = r(3)r(2)r(1)r(0) = 1234 ```

## The Javascript/Html Code

 ``` Binary To Decimal

Binary To Decimal

```