Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Beginners » Revisions
 

From one to another number system

, 2 Apr 2012
Rate this:
Please Sign up or sign in to vote.
Developing and implementation of algorithm for converting number from one to another number system
This is an old version of the currently published article.

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*23 + 0*22 + 0*21 + 1*20 + 1*2-1 + 1*2-2 + 1*2-3 = 9,875(10)

numerical system with three digits :

1020,021(3) = 1*33 + 0*32 + 2*31 + 0*30 + 0*3-1 + 2*3-2 + 1*3-3 = 33,2592592592593(10)

numerical system with four digits :

1230,301(4) = 1*43 + 2*42 + 3*41 + 0*40 + 3*4-1 + 0*4-2 + 1*4-3 = 108,765625(10)

...........................................................................................................................................................................

...........................................................................................................................................................................

...........................................................................................................................................................................

numerical system with ten digits :

9870,567(10) = 9*103 + 8*102 + 7*101 + 0*100 + 5*10-1 + 6*10-2 + 7*10-3= 9870,567(10)

numerical system with sixteen digits:

8FEA,CD7(10) = 8*163 + 15*162 + 14*161 + 10*100 + 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(n-1)a(n-2)...a(1)a(0), c1c2c3...cn)(x) = Sum( ak x+k )(k=n down to k =0) + Sum( ck 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 :


  1. Start program

  2. Enter number you want to convert

  3. Enter the base of the number system of the entered number

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

  5. Separate Integral part of number from Fractal part

  6. Convert Integral part ( go to subroutine step 1)

  7. Convert Fractal part ( go to subroutine step 1)

  8. Join two converted parts

  9. Show result

  10. End program


  1. Convert Integral part subroutine

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

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

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

  5. Sum = Sum + ak * x+k

  6. Decrease value of K by one ( K = K - 1)

  7. If K is greater then -1 go to step 4

  8. End of subroutine


  1. Convert Fractal part subroutine

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

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

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

  5. Sum = Sum + ck * x-k

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

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

  8. 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)

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

  2. 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

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

  4. Multiply Integral Of Division Result with Base

    Integral Of Division Result * Base = Result

    256 * 2 = 512

  5. Subtract Result and Integral and remember it as a sequence of Ciphers

    512 – 512 = 0

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

  7. If Integral not equal zero go to step 2

  8. 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

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

  2. Multiply Fractal and Base number

    Result Of Multiplication = Fractal * Base

    0,125 * 2 = 0,250

  3. Separate Integral part of result of multiplication and remember it as Cipher sequence

    Integral part of result = 0

  4. New Fractal = 0,250

  5. If Fractal is not equal 0 go to step 2

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

  2. 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.

//
// declaration of variables
//
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()
{
//
// Resolving conversion from any number system to Decimal
//

//
// If it is Float number, split number to Decimal and Float part
// exmpl. 11.011(2) DecimalPart = 11 and FloatPart = 011
//
// The conversion shall take the place even if the number is too large
// exmpl : 10101010101010101010101010101010101010,111 (2) equals 183251937962,875 (10)
// 
//

Change = false;
if (IsFloat == true)
{
DecimalPart = "";
FloatPart = "";
RealNumber = 0;
DecimalFloat = textBox1.Text.Split(',');
DecimalPart = DecimalFloat[0];
FloatPart = DecimalFloat[1];
// 
// Decimal part first
//
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);
}
//
// Float part second
//
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;
}

//
// If it is Decimal number
// exmpl. 1001
//
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

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

Perić Željko
Software Developer
Serbia Serbia
Serbia
Smederevo
01.10.2011
 
personal data:
 
Name : Željko
Surname : Perić
Country : Serbia
e-mail : periczeljkosmederevo@yahoo.com
 
Home page in Serbian and English
 
http://sites.google.com/site/periczeljkosmederevo/home
https://sites.google.com/site/periczeljkosmederevoenglish/
 
Educational background :
 
I have finished high school EC "Milentije Popović" in Smederevo (Serbia), technical and mathematical direction of natural science, profession mathematical programming assistant. Computer programming, development of algorithms, etc.
 
The most frequently used tool that I use for programming computer is IDE Sharp Develop, programming languge C#.

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
GeneralMy vote of 5 Pinmembercsharpbd17-Mar-14 9:46 
GeneralMy vote of 1 PinmemberMember 811845415-Jul-13 2:52 
GeneralMy vote of 5 PinmemberArash M. Dehghani14-Jul-13 11:23 
GeneralRe: My vote of 5 PinmemberPerić Željko15-Jul-13 0:08 
GeneralThoughts PinmemberPIEBALDconsult9-May-12 3:48 
GeneralRe: Thoughts PinmemberPerić_Željko9-May-12 9:13 
GeneralRe: Thoughts PinmemberPIEBALDconsult10-May-12 3:00 
GeneralRe: Thoughts PinmemberPerić_Željko10-May-12 6:15 
GeneralMy vote of 5 Pinmembercjb1109-May-12 2:54 
AnswerRe: My vote of 5 PinmemberPerić_Željko9-May-12 9:07 
QuestionC? PinmemberToli Cuturicu8-May-12 22:22 
AnswerRe: C? PinmemberPerić_Željko9-May-12 9:39 
GeneralRe: C? PinmemberToli Cuturicu9-May-12 10:51 
GeneralRe: C? [modified] PinmemberPerić_Željko9-May-12 11:28 
GeneralRe: C? PinmemberPerić_Željko10-May-12 7:50 
QuestionDecimal Part Separator Pinmemberchaau8-May-12 17:36 
AnswerRe: Decimal Part Separator PinmemberPerić_Željko8-May-12 18:45 
AnswerRe: Decimal Part Separator PinmemberPerić Željko15-Jul-13 0:49 
QuestionNumber System Conversion Pinmemberdelyk8-Apr-12 6:18 
AnswerRe: Number System Conversion PinmemberPerić_Željko8-Apr-12 10:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 2 Apr 2012
Article Copyright 2012 by Perić Željko
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid