Google
 

Converting Decimal Integer Numbers  

This tool is provided as an implementation of conversion algorithms based on division method modeled as Java Scripts. It converts integer decimal numbers to their equivalent in Binary, Octal, and Hexadecimal.

The equivalent of the Integer number:

; example: 2007)

IS:
(division method)



The generalized division algorithm for converting decimal integers in other bases is:

Integer. Given an integral number NI(b) it can be represented in a new base q as:
NI(b) =a′n*qn+a′n-1*qn-1+ ... + a′0*q0
where a′i are digits expressed in the base q (i=0,1,2,...,n). In order to convert the number we must determine the value of the coefficients a′i in the base q.
These coefficients are obtained by repeatedly division of the number NI(b) to the base q, as:
NI(b) / q =a′n*qn-1+a′n-1*qn-2+ ... +a′1*q0+a′0a remainder a′0(q) and a quotient

N'I(b)=a′n*qn-1+a′n-1*qn-2+...+a′1*q1-1.

            By division with q we obtain a remainder a′1(q)  and a new quotient. We continue the division process until we determine a′n(q) (until the quotient is 0).

            The digit a′n(q) is the most significant digit of the number in the new base q and a′0(q) is the most unsignificant digit of the number in the new base q.

The generalized algorithm for this method is:

Step1.

Divide the number to the new base and obtain a quotient and a remainder. The remainder just obtained is a digit in the new base;

Step2.

If the quotient is not 0
    Then
take in account the quotient as a new number and execute Step1;
    Else
            the conversion process stops.

The coefficients are written in reverse order they obtained and the number is the equivalent in the new base: a′n a′n-1 ... a′0(q).

Implementation comments

The implementation of the algorithm converts integers from decimal to Binary, Octal, and Hexadecimal bases and exploits the way integers represented in the computers memory (sign magnitude notation) . For that reason it not divides to the new base. Because the integral number represented as a bit-string (binary) and we convert this in Binary, Octal, and Hexadecimal we replace the division with a «shift right» a number of times equal to the power of two of the base and insulates the new coeficient by applying a logical AND between the number and the (NewBase-1).

/* The memory variable Digits contains the digits for decimal, binary, octal and hexadecimal numbering systems */

var Digits="0123456789abcdef";

/* The name of a specific conversion function is dec-to-NewBaseName (example: dec2bin, meaning decimal-to-binary)
decimalNumber - contains the decimal integer passed in as argument
The variable Base contains the new base in decimal (example: 16 for hexadecimal, 8 for octal, 2 for binary etc) */

function dec2NewBase(decimalNumber)
{


/* Defines a variable to store the converted number and in the meantime places in that the rightmost digit of the converted number determined by applying a logical and between the (Base - 1) and the original number (anding)*/
var convertedNumber = Digits.substr(decimalNumber & (Base-1), 1);
/* If the number is greather than the (Base-1) (the largest digit in the numbering system) the division have sense and we continue by determining the next digit - a new cycle). It allows determining a new integer */
while(decimalNumber > (Base-1))
{

/* Determine the new integer by shifting the number to the right a number of times equal with the power of two of the new base (1 for binary; 3 for octal; 4 for hexadecimal ...*/
d >>= PowerOfTwo;
/* Pick up the rightmost digit from the right and place this before all others previously defined (the new digits mus be written in reverse order they determined)*/
convertedNumber = Digits.substr(d & (Base-1), 1) + convertedNumber;
}

/* Return the converted number to the caller */
return convertedNumber;
}


 
*)The theory in that page is made by excerpts from the books:
- Vasile Avram, Gheorghe Dodescu: Informatics: Computer Hardware and Programming in Visual Basic, Ed. Economică, Bucureşti, 2003
- Vasile Avram, Gheorghe Dodescu: General Informatics, Ed. Economica, 1997

Copyright © 2006-2008 Vasile Avram

Valid CSS! Valid HTML 4.01 Transitional Cynthia Tested!