Introduction
Program to convert numbers from one to another number system was written as "Windows Form" application in Sharp Develop IDE, programming language C, and for his work is essential net framework 2.0 or higher. Graphical user interface (GUI) is intuitive and consists of a field, a text box, to enter the number you wish to convert, a text box to show the results, or converted number, four buttons to increase or decrease the basis of numerical system, to get the number in desired numerous system, and three text messages, text Labels to show the change of numerical system. The program has a limit when the size of the entered number for conversion is in question. This limitation is caused by the limitations in the programming language. It is the maximum number of digits that program can accept when converting from decimal to other number system. So the maximum number of digits that entered number in decimal system may have is 15 , including decimal point. As for conversion from other systems in decimal, number is limited to 256 digits. Program code with algorithm is explained in this article , at the bottom are download links for downloading full solution for IDE Sharp Develop, and program. Picture of program is also link to download program.
Introduction
Mathematics is always interesting scientific discipline with its own problems which are solved by computer. "Conversion of numbers from one to another number system" is one of the subjects that can be used for learning how to develop an algorithm and computer program for solving this and other similar problems.
Since I comply with a general method of solving problems with computers and this problem will be solved through specific steps.
Step one
Complete overview and understanding of the process for which you want to create a computer program, or write a
Pseudo code.
Step two
Writing the
Algorithm based on a pseudo code.
Step three
Coding on the basis of the algorithm, in some of the programming language that is most suitable for this type of program.
Step four
Testing and debugging program.
Step five
Writing documentation for the program, instruction, license agreement and patenting of software.
Solving problem
Step one
Research and a thorough understanding of the process of conversion of numbers from one to another number systems.
Searching all over the internet for information on how to convert number from one to another number system, I have found many interesting links and articles relating to the subject. What is most important , all of them explain process of conversion trough simple mathematical examples, understandable to anyone who have basic knowledge in mathematic. I have chosen three articles on
which I have based my research and development of
Pseudo code.
First “Number system” , that explains everything about numbers,
Second
“Numerical system” , that explains everything about numeral systems, and
Third
"Positional notation" , that explains thoroughly positional numeral systems , including conversion.
Analise those articles thoroughly, especially the last one. Here is extracted the essence that is needed for problem solving.
Conversion from chosen number system to decimal number system ( * is sign for multiplication) :
numerical system with two digits :
1001,111_{(2) }= 1*2^{3} + 0*2^{2} + 0*2^{1} + 1*2^{0 }+ 1*2^{1} + 1*2^{2 }+ 1*2^{3 }= 9,875_{(10)}
numerical system with three digits :
1020,021_{(3) }= 1*3^{3} + 0*3^{2 }+ 2*3^{1} + 0*3^{0 }+ 0*3^{1} + 2*3^{2 }+ 1*3^{3 }= 33,2592592592593_{(10)}
numerical system with four digits :
1230,301_{(4)} = 1*4^{3} + 2*4^{2 }+ 3*4^{1} + 0*4^{0 }+ 3*4^{1} + 0*4^{2 }+ 1*4^{3 }= 108,765625_{(10)}
_{...........................................................................................................................................................................}
_{...........................................................................................................................................................................}
_{...........................................................................................................................................................................}
numerical system with ten digits :_{ }
9870,567_{(10)} = 9*10^{3} + 8*10^{2 }+ 7*10^{1} + 0*10^{0 }+ 5*10^{1} + 6*10^{2 }+ 7*10^{3}= 9870,567_{(10)}
numerical system with sixteen digits:
8FEA,CD7_{(10)} = 8*16^{3} + 15*16^{2 }+ 14*16^{1} + 10*10^{0 }+ 12*16^{1} + 13*16^{2 }+ 7*10^{3}= 36842,8024902344_{(10)}
Here we can see the basic steps of converting numbers with number system base X to decimal number system :
(a_{(n)}a_{(n1)}a_{(n2)}...a_{(1)}a_{(0)}, c_{1}c_{2}c_{3}...c_{n})_{(x) }= Sum( a_{k}^{ }x^{+k })_{(k=n down to k =0)}^{ }+ Sum( c_{k }x^{k })_{(k=1 to k=n)}
Notice that the result of conversion to decimal number system is exact only if number is rational,
otherwise it is approximate because of rounding to the desirable number of decimal places.
There is also the limitation of number of decimal places by the programming language.
Pseudo code for program for converting numbers from any to decimal number system goes like this :

Start program

Enter number you want to convert

Enter the base of the number system of the entered number

If it is not float point number Convert Integral part ( go to subroutine step 1). Go to step 9.

Separate Integral part of number from Fractal part

Convert Integral part ( go to subroutine step 1)

Convert Fractal part ( go to subroutine step 1)

Join two converted parts

Show result

End program

Convert Integral part subroutine

Assign value for X ( X = the base of the number system of the entered number)

Assign value for K ( K = n, goes from n down to 0)

Assign value for A ( A = digit in Integral part of number at K position)

Sum = Sum + a_{k * }x^{+k }

Decrease value of K by one ( K = K  1)

If K is greater then 1 go to step 4

End of subroutine

Convert Fractal part subroutine

Assign value for X ( X = the base of the number sistem of the entered number )

Assign value for K ( K = 1 , goes from 1 to n)

Assign value for C ( C = digit in Fractal part of number at K position)

Sum = Sum + c_{k * }x^{k }

Increase value of K by one ( K = K + 1)

If K is less then n+1 go to step 4

End of subroutine
Conversion from decimal number system to another number system.
Convert decimal number 512,125 to numerical system with two digits, known as Binary system.
(512,125)_{(10)} = ( ? )_{(2)}

Separate Integral part from Fractal part of number. (Integral = 512 , Fractal = 0,125)

Divide Integral part of number with Base of number system that you want to convert number in.
Binary number system Base is two.
Integral / Base = Result Of Division
512 : 2 = 256,0

Separate Integral part of Result Of Division, Integral Of Division Result = 256

Multiply Integral Of Division Result with Base
Integral Of Division Result _{* }Base = Result
256 * 2 = 512

Subtract Result and Integral and remember it as a sequence of Ciphers
512 – 512 = 0

New Integral = 256 ( Integral = Integral Of Division Result)

If Integral not equal zero go to step 2

Read Cipher sequence in oposit direction and get Binary number.
The result of conversion is 512_{(10)} = 1000000000_{(2)}
The whole process is better understandable trough this table, just follow the header of the table.
A

B

C

D

E

F

Integral part of number

Base to convert in
( 2 )

Integral part of result of division
Integral / Base
A / B

Result of multiplication Integral * Base
C _{*} B

Result of subtraction
Cipher sequence
D  A

Read Cipher sequence in oposit direction and get Binary number
Converted Integral part of number to binary number system 512_{(10)} = 1000000000_{(2)}

512

2

256

512

0

1

256

2

128

256

0

0

128

2

64

128

0

0

64

2

32

64

0

0

32

2

16

32

0

0

16

2

8

16

0

0

8

2

4

8

0

0

4

2

2

4

0

0

2

2

1

2

0

0

1

2

0

0

1

0

Process of converting Fractal part of number 0,125_{(10)} is shown below in table .
A

B

C

D

E

Fractal part of number

Base to convert in

Result of multiplication
Fractal _{*} Base
A _{*} B

Integral part of result of multiplication Cipher sequence

Converted Integral part of number to binary number system 0,125_{(10)} = 0,001_{(2)}

0,125

2

0,250

0

0

0,250

2

0,5

0

0

0,5

2

1,0

1

0

0,250

2

0,5

0

0

0,5

2

1,0

1

1


Separate Fractal part of number ( Fractal = 0,125 )

Multiply Fractal and Base number
Result Of Multiplication = Fractal ^{*} Base
0,125 _{*} 2 = 0,250

Separate Integral part of result of multiplication and remember it as Cipher sequence
Integral part of result = 0

New Fractal = 0,250

If Fractal is not equal 0 go to step 2

Read Cipher sequence and get Binary number ( the direction of reading ciphers is the same as Cipher sequence )

The result of conversion is 0,125_{(10)} = 0,001_{(2)}
Now we have converted Integral and Fractal part of number and when we join them together we get exact result :
( 512,125 )_{(10)} = ( 1000000000,001 )_{(2)}
If we want to check if the result is correct , we shall use the conversion method from any to decimal number system :
( 1000000000,001 )_{(2)}=
= 1_{*}2^{(9)}+0_{*}2^{(8)}+0_{*}2^{(7)}+0_{*}2^{(6)}+0_{*}2^{(5)}+0_{*}2^{(4)}+0_{*}2^{(3)}+0_{*}2^{(2)}+0_{*}2^{(1)}+0_{*}2^{(0)}+0_{*}2^{(1)}+0_{*}2^{(2)}+1_{*}2^{(3) }= ( 512,125 )_{(10)}
The method for conversion of numbers from one to another number system is the same for all positional number systems, except for the number systems that have greater base than 10. Pseudo code is different, because of symbols used to represent numbers. All number systems from Binary to Decimal , use these symbols ( 0,1,2,3,4,5,6,7,8,9) and they are understandable to computer as digits or numbers from decimal number system, but number system as for example Hexadecimal, with base sixteen use ( A,B,C,D,E,F ) as symbols for numbers ( 10,11,12,14 and 15 ), so it is most important to have subroutine for conversion of those symbols to decimal numbers before starting conversion. Also it is necessary to have subroutine for conversion of numbers ( 10,11,12,14 and 15 ) to ( A,B,C,D,E,F ) if we convert number from Decimal to Hexadecimal number system.
Step two
Writing the algorithm based on a pseudo code.
When you finish writing pseudo code for program it is easy to write
Algorithm. Here is only the general algorithm for converting numbers from any to Decimal where you can see the program flow and main subroutines. What is necessary to do , is to write algorithms for the subroutines in the general algorithm : Separate Integral from Fractal part , Convert Integral part, Convert Fractal part , Join two converted parts.
Step three
Coding on the basis of the algorithm, in some of the programming language that is most suitable for this type of program.
Writing program based on the algorithm shown above is simple. Just follow the steps in algorithm. Here you can see the part of main program for conversion from any to Decimal number system that is explained trough pseudo code and trough algorithm. Reading the program code that was written by some other programmer could be difficult, and usually hard for understanding, especially if it is only the fragments as shown below. So it is recommended to see complete code , download full solution for IDE Sharp Develop 4.1 with open sorce code written in C. There you can see complete code for program.
int EnteredNumberBaseMax = 0;
int EnteredNumberBaseMin = 0;
int EnteredNumberBase = 0;
bool EntryIsNumber = false;
const int ConvertToNumberBaseMax = 16;
const int ConvertToNumberBaseMin = 2;
int ConvertToNumberBase = 2;
string [] DecimalFloat = new string [2];
string DecimalPart = "";
string FloatPart = "";
bool IsFloat = false;
int Index = 0;
int Lenght = 0;
int Exponent = 0;
int CiferSelect = 0;
double BaseExponent = 0;
double Cipher = 0;
double RealNumber = 0;
double DecimalNumber = 0;
double FloatNumber = 0;
double D = 0;
double F = 0;
double Integral = 0;
double DivisionResult = 0;
double MultiplicationResult = 0;
string Letter = "";
string ConvertedToChosenNumberSistem = "";
bool Change = false;
void ConvertToDecimal()
{
Change = false;
if (IsFloat == true)
{
DecimalPart = "";
FloatPart = "";
RealNumber = 0;
DecimalFloat = textBox1.Text.Split(',');
DecimalPart = DecimalFloat[0];
FloatPart = DecimalFloat[1];
Exponent = 0;
BaseExponent = 0;
Cipher = 0;
D = 0;
Index = 0;
Lenght = 0;
Letter = "";
Lenght = DecimalPart.Length;
for(Index=0;Index<Lenght ;Index=Index+1)
{
Exponent = Lenght  Index  1;
Letter = DecimalPart.Substring(Index,1);
SelectValueForCipher();
BaseExponent = double.Parse(Math.Pow(EnteredNumberBase,Exponent).ToString());
D = D + (Cipher * BaseExponent);
}
Exponent = 0;
BaseExponent = 0;
Cipher = 0;
F = 0;
Index = 0;
Lenght = 0;
Letter = "";
Lenght = FloatPart.Length;
for(Index=0;Index<Lenght ;Index=Index+1)
{
Exponent = Index+1;
Letter = FloatPart.Substring(Index,1);
SelectValueForCipher();
BaseExponent = double.Parse(Math.Pow(EnteredNumberBase,Exponent).ToString());
F = F + (Cipher * BaseExponent);
}
RealNumber = D + F ;
Change = true;
}
else if (IsFloat == false)
{
DecimalPart = "";
Cipher = 0;
D = 0;
Exponent = 0;
RealNumber = 0;
Index = 0;
Lenght = 0;
Letter = "";
DecimalPart = textBox1.Text;
Lenght = DecimalPart.Length;
for(Index=0;Index<Lenght ;Index=Index+1)
{
Exponent = Lenght  Index  1;
Letter = DecimalPart.Substring(Index,1);
SelectValueForCipher();
BaseExponent = double.Parse(Math.Pow(EnteredNumberBase,Exponent).ToString());
D = D + (Cipher * BaseExponent);
}
RealNumber = D;
Change = true;
}
MessageBox.Show("Result :" + RealNumber.ToString());
}
void SelectValueForCipher()
{
switch (Letter)
{
case "0": Cipher = 0;
break;
case "1": Cipher = 1;
break;
case "2": Cipher = 2;
break;
case "3": Cipher = 3;
break;
case "4": Cipher = 4;
break;
case "5": Cipher = 5;
break;
case "6": Cipher = 6;
break;
case "7": Cipher = 7;
break;
case "8": Cipher = 8;
break;
case "9": Cipher = 9;
break;
case "A": Cipher = 10;
break;
case "B": Cipher = 11;
break;
case "C": Cipher = 12;
break;
case "D": Cipher = 13;
break;
case "E": Cipher = 14;
break;
case "F": Cipher = 15;
break;
}
}
Step four
Testing and debugging program.
General rule for testing program is : “ Never test program by Your self, it should be done by fellow programmer”.
Reason for that is that you usually would not see all errors in code , especially logical type of errors, and it is faster. Debugging program upon found errors is easy. This step should be repeated until no error is found, and program is functioning correct on the basis of beta tester (fellow programer) reports.
Step five
Writing documentation for the program, instruction, license agreement and patenting of software.
Documentation for program is similar to this article , only it should be far more specific in explanation of development of pseudo code, algorithm and program code. Instructions or user manual can be written in many different programs for creating user manuals or depending on complexity of program it can be written as simple *.txt document. There are different types of licence agreement. See the articles on these adreses :
Licence
Licence agreement
Software patent
Points of Interest
Since I am not a member of
Code Project for a long time, I have not read many questions of other members , but I have realized that most of the questions was asked by the beginners in programming. Given that each question requires an adequate answer I chose to create this article that should become my general solution to a problem that has been very interesting to me and to other members of Code Project. Also it is intended to be an article from which every beginner can learn something new and interesting in development of algorithms and coding in C.
History
Last revision 01.04.2012 Author.
Download links
Download full solution for IDE Sharp Develop 4.1 with open sorce code written in C. After downloading solution you can open it in IDE Sharp Develop and Build the release version of program. If you work in other type of IDE ( integrated development environment), you can open all documentation that contains sorce code, and simply copy it into your program.
Download solution with open source code
If you just want to use program you can download it here. After downloading just click on executable file. Important , program needs net framework 2.0 or higher to work correctly.
Download program
Licence
This article is public domain. Solution that is developed in IDE Sharp Develop , is covered with GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999. Executable version of program is free.You can freely copy and distribute it if you want.
Author
All about the autor you can find at this adress.
Home page