Concepts of Conversion of Base 2 Fixed Register Binary Number to Base 10 Decimal

Sean Rodby, Luke Snyder, Autar Kaw
University of South Florida
United States of America
kaw@eng.usf.edu
Website: http://numericalmethods.eng.usf.edu
Version: Mathematica 6; Last Revised: August 28, 2008

Initialization

Clearing the definitions of all symbols in the current context:

ClearAll[Evaluate[Context[]<>"*"]]

Introduction

The following worksheet illustrates how to convert a fixed point register binary (base-2) number to a decimal number (base-10) using loops and various conditional statements. The user inputs a binary number in the Input section of the program, and then an equivalent decimal number is given as an output.

Section 1: Input Data

This is the only section where the user interacts with the program.

Enter number to be converted to decimal number

binnum="111111101.11";

This is the end of the user section. All information must be entered before proceeding to the next
section. RE-EVALUATE THE NOTEBOOK.

Section 2: Procedure

The ToExpression command is used to take the Mathematica string binnum and parse the string as if it were a Mathematica input The floor command is used to isolate the integer part of the base-2 number. Then the length command determines the length of the entire binary number and all the characters in the string.

intbin=Floor[ToExpression[binnum]];
n=StringLength[binnum];
strint=ToString[intbin];
m=StringLength[strint];

Using a loop to sum values of the integer portion of the base-2 number. The loop variable sumint is used for summation and is initialized at 0.

sumint=0
Do[
(Subscript[binint, i])=ToExpression[StringTake[strint,{i}]];
sumint=sumint+(Subscript[binint, i])*(2^(m-i)),
{i,1,m}]

Using a loop to sum values of the fractional portion of the base-2 number. The loop variable sumfrac is used for summation and is initialized at 0. Note that the starting point in this loop is the length of the integer portion (m), plus 2 which effectively skips the decimal point in the character array.

sumfrac=0;
j=1;
Do[
(Subscript[binfrac, j])=ToExpression[StringTake[binnum,{i}]];
(Subscript[binfracstr, j])=StringTake[binnum,{i}];
sumfrac=N[sumfrac+(Subscript[binfrac, j])*(2^(-j)),10];
j=j+1,
{i,m+2,n}]

Adding the fractional portion of the base-2 number with the integer portion which yields the base-10 number.

totaldec=N[sumint+sumfrac,10]

Conclusion

This worksheet illustrates the use of Mathematica to convert a base-2 binary number to a base-10 number. It is important to understand the binary system as it has numerous applications. Critical to this understanding is being able to convert decimal numbers to binary numbers, and vice-versa.

References

Binary Representation of Numbers.
See: http://numericalmethods.eng.usf.edu/nbm/gen/01aae/nbm_gen_aae_txt_binaryrepresentation.pdf

Legal Notice: The copyright for this application is owned by the author(s). Neither Wolfram Research nor the author(s) are responsible for any errors contained within and are not liable for any damages resulting from the use of this material. This application is intended for non-commercial, non-profit use only. Contact the author for permission if you wish to use this application in for-profit activities.