# From one to another number system

By , 8 May 2012

## Overview

Program to convert numbers from one to another number system was written as "Windows Form" application in Sharp Develop IDE, programing 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.

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.

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.

## 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 Integral and Result and remember result 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 (condition for stopping process of conversion)

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 A - D 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 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 (condition for stopping process of conversion)

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,13,14 and 15 ) to ( A,B,C,D,E,F ) if we convert number from Decimal to Hexadecimal number system.

One more rule :

When converting numbers from one to another number system , it is usual to convert number first to Decimal and then to any other number system.

As for the shown pseudo code it is far from perfect. There is need to make pseudo code for the part of the program for entering and verification of the entered number , it is necessary to limit the size of the entered number, because of the limitations of a programming language. We have to create a part of pseudo code for conversion of those symbols that are used in numeral systems with base greater then ten, limit the maximum and minimum values for system bases, and we need to cover the part where are shown info messages to user (wrong entry , etc...).

Those parts of pseudo code are simple so there is no need to write it down , they shall be implemented in coding directly.Just watch out, not to skip it when writing algorithm.

Writing the Algorithm based on a pseudo code.

When you finish writing pseudo code for program it is easy to write Algorithm. Since the program was written as a Windows Form application with a graphical user interface that uses many different controls, and the algorithm is suited to this type of work environment.

In program we use :

TextBox1 control for entering number to convert

TextBox2 control for showing the result of conversion

Button1 and Button2 for increasing and decreasing value of entered number system base

Button3 and Button4 for increasing and decreasing value of chosen number system base to convert number in

Label controls for showing entered number system base and chosen number system base to convert number in

There are many subroutines that are used in program and the main are :

ConvertToDecimal , that converts Entered Number to Decimal system , and also calls the next sub if it is necessary upon condition.

ConvertDecimalToDifferentNumberBase , that converts Entered Number from Decimal to chosen number system.

Other subroutines are not explained but they are simple and understandable simply by looking the program code.

With those controls and subroutines we create whole program based on the pseudo code.Each of this controls have its own event handler that is used in program and its own algorithm accordingly.Here are shown general algorithms for solving problem of conversion. Their generality is reflected in the fact that we have not declared all the variables, their names and types, which will be used within the program. Declaration of variables is usually the rule, when writing the algorithms.

First event handler is TextBox1Leave , that is raised when user enter number in TextBox1 field and types Enter or clicks outside of control so the control loses focus.

Second event handler is Button1Click , that is raised when user clicks on button and increase value for entered number system base.It is the same for Button2Click. that is raised when user clicks on button and decrease value for entered number system base.

Third event handler is Button3Click , that is raised when user clicks on button and increase value for chosen number system base.It is the same for Button4Click. that is raised when user clicks on button and decrease value for chosen number system base. In this case, when user changes value for base of number sistem to convert number in, the “Convert to Decimal” subroutine shall call automatically subroutine ConvertDecimalToDifferentNumberBase , that converts Decimal Number to chosen system.

Next is subroutine Convert To Decimal. It has two parts that needs to be worked out based on the pseudo code : Convert Integral part of number and Convert Fractal part of number.

Last algorithm that is shown in this article is created for subroutine Convert Decimal To Different Number Base. It also has two parts that needs to be worked out based on the pseudo code : Convert Integral part of number and Convert Fractal part of number.

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 algorithms shown above is simple. Just follow the steps in algorithm. Here you can see the part of program for conversion from one to another number system that is explained trough pseudo code and trough algorithms. 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.

```/*
* Created by SharpDevelop.
* User: Perić Željko
* Date: 08.02.2012
* Time: 10:46
*
*  Simple Windows Form Application for converting float point numbers
*  from one to another number system
*
*  If you want to convert numbers from decimal to other number systems
*  number to convert can have maximum 15 digits including decimal separator
*  exmpl: 123456789,12345 (10), so the largest number is 999999999999999 (10)
*
* If you want to convert numbers from any to decimal number system,
* number can have 256 digits
*
*
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace From_one_to_another_number_system
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
//
// 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 CipherSelect = 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;

public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
}

void TextBox1Leave(object sender, EventArgs e)
{
//
// What is the number entered
// What is the possible number system base of entered number
//

//
// declaration of variables
//

string Text = "";
string Letter = "";
string CipherDigits = "1234567890ABCDEF,"; // Decimal Float separator is “ , “
int TextLenght = 0;
int LetterCounter = 0;
int CommaCounter = 0;

EnteredNumberBaseMax = 0;
EnteredNumberBaseMin = 2;

//
// Is it number and what type
//
if ( textBox1.Text == "" )
{
textBox1.Text = "2011,2012";
}
textBox1.Text = textBox1.Text.ToUpper();
textBox1.Refresh();
Text = textBox1.Text;
TextLenght = Text.Length;
while ( LetterCounter < TextLenght )
{
Letter = Text.Substring( LetterCounter,1 );
if  ( CipherDigits.Contains(Letter) )
{
EntryIsNumber = true;
switch (Letter)
{
case "0" : if ( EnteredNumberBaseMax < 2 )
{
EnteredNumberBaseMax = 2;
}
break;
case "1" : if ( EnteredNumberBaseMax < 2 )
{
EnteredNumberBaseMax = 2;
}
break;
case "2" : if ( EnteredNumberBaseMax < 3 )
{
EnteredNumberBaseMax = 3;
}
break;
case "3" : if ( EnteredNumberBaseMax < 4 )
{
EnteredNumberBaseMax = 4;
}
break;
case "4" : if ( EnteredNumberBaseMax < 5 )
{
EnteredNumberBaseMax = 5;
}
break;
case "5" : if ( EnteredNumberBaseMax < 6 )
{
EnteredNumberBaseMax = 6;
}
break;
case "6" : if ( EnteredNumberBaseMax < 7 )
{
EnteredNumberBaseMax = 7;
}
break;
case "7" : if ( EnteredNumberBaseMax < 8 )
{
EnteredNumberBaseMax = 8;
}
break;
case "8" : if ( EnteredNumberBaseMax < 9 )
{
EnteredNumberBaseMax = 9;
}
break;
case "9" : if ( EnteredNumberBaseMax < 10 )
{
EnteredNumberBaseMax = 10;
}
break;
case "A" : if ( EnteredNumberBaseMax < 11 )
{
EnteredNumberBaseMax = 11;
}
break;
case "B" : if ( EnteredNumberBaseMax < 12 )
{
EnteredNumberBaseMax = 12;
}
break;
case "C" : if ( EnteredNumberBaseMax < 13 )
{
EnteredNumberBaseMax = 13;
}
break;
case "D" : if ( EnteredNumberBaseMax < 14 )
{
EnteredNumberBaseMax = 14;
}
break;
case "E" : if ( EnteredNumberBaseMax < 15 )
{
EnteredNumberBaseMax = 15;
}
break;
case "F" : if ( EnteredNumberBaseMax < 16 )
{
EnteredNumberBaseMax = 16;
}
break;
case "," : CommaCounter = CommaCounter + 1;

break;
}
LetterCounter = LetterCounter + 1;
}
else
{
EntryIsNumber = false;
LetterCounter = TextLenght;
}
}

//
//
if (CommaCounter > 1 | EnteredNumberBaseMax == 0 | EntryIsNumber == false)
{
WriteErrorMessage();
}
else
{
ClearZeroFromNumber();
switch (EnteredNumberBaseMax)
{
case 2 :
{
WriteMessage();
ConvertToDecimal();
}
break;
case 3 :
{
WriteMessage();
ConvertToDecimal();
}
break;
case 4 :
{
WriteMessage();
ConvertToDecimal();
}
break;
case 5 :
{
WriteMessage();
ConvertToDecimal();
}
break;
case 6 :
{
WriteMessage();
ConvertToDecimal();
}
break;
case 7 :
{
WriteMessage();
ConvertToDecimal();
}
break;
case 8 :
{
WriteMessage();
ConvertToDecimal();
}
break;
case 9 :
{
WriteMessage();
ConvertToDecimal();
}
break;
case 10 :
{
WriteMessage();
ConvertToDecimal();
}
break;
case 11 :
{
WriteMessage();
ConvertToDecimal();
}
break;
case 12 :
{
WriteMessage();
ConvertToDecimal();
}
break;
case 13 :
{
WriteMessage();
ConvertToDecimal();
}
break;
case 14 :
{
WriteMessage();
ConvertToDecimal();
}
break;
case 15 :
{
WriteMessage();
ConvertToDecimal();
}
break;
case 16 :
{
ConvertToDecimal();
}
break;
}
}
}

void TextBox1KeyPress(object sender, KeyPressEventArgs e)
{
//
// If user clicked "Enter", inside textBox1
//
if (e.KeyChar == (char)Keys.Return)
{
label8.Focus();
}

}

void Button1Click(object sender, EventArgs e)
{
//
//  Increasing of  base for the entered number
//
if (EntryIsNumber == true)
{
if (EnteredNumberBase < EnteredNumberBaseMax)
{
Change = true;
EnteredNumberBase = EnteredNumberBase + 1;
label6.Text = "( " + EnteredNumberBase.ToString() + " )";
ConvertToDecimal();
if (Change == false)
{
EnteredNumberBase = EnteredNumberBase - 1;
label6.Text = "( " + EnteredNumberBase.ToString() + " )";
WriteE1Message();
}
}
}
}

void Button2Click(object sender, EventArgs e)
{

//
//  Decreasing of  base for the entered number
//
if (EntryIsNumber == true)
{
if (EnteredNumberBase > EnteredNumberBaseMin)
{
Change = true;
EnteredNumberBase = EnteredNumberBase - 1;
label6.Text = "( " + EnteredNumberBase.ToString() + " )";
ConvertToDecimal();
if (Change == false)
{
EnteredNumberBase = EnteredNumberBase + 1;
label6.Text = "( " + EnteredNumberBase.ToString() + " )";
WriteE2Message();
}
}
}
}

void Button3Click(object sender, EventArgs e)
{
//
// Decreasing of  base for number sistem that number shall be converted in
//
if (EntryIsNumber == true)
{
if (RealNumber.ToString().Contains("E") == true)
{
WriteE3Message();
}

else if (ConvertToNumberBase > ConvertToNumberBaseMin)
{
ConvertToNumberBase = ConvertToNumberBase - 1;
label7.Text = "( " + ConvertToNumberBase.ToString() + " )";
label8.Text =  "( " + ConvertToNumberBase.ToString() + " )";
ConvertToDecimal();
}
}
}

void Button4Click(object sender, EventArgs e)
{
//
// Increasing of base for number sistem that number shall be converted in
//
if (EntryIsNumber == true)
{
if (RealNumber.ToString().Contains("E") == true)
{
WriteE4Message();
}

else if (ConvertToNumberBase < ConvertToNumberBaseMax)
{
ConvertToNumberBase = ConvertToNumberBase + 1;
label7.Text = "( " + ConvertToNumberBase.ToString() + " )";
label8.Text =  "( " + ConvertToNumberBase.ToString() + " )";
ConvertToDecimal();
}
}
}

void ClearZeroFromNumber()
{
//
// Clears number from redundant zeroes if it is number with float point
// with all possibilites
//
// exmpl: 0000101010,BCDFFF0000 equals 101010,BCDFFF
//
textBox1.Text = textBox1.Text.ToUpper();
textBox1.Refresh();
IsFloat = false;
if ( textBox1.Text.Contains(","))
{
IsFloat = true;
DecimalFloat = textBox1.Text.Split(',');
DecimalPart = DecimalFloat[0];
FloatPart = DecimalFloat[1];

DecimalPart = DecimalPart.TrimStart('0');
if (DecimalPart =="")
{
DecimalPart = "0";
}

FloatPart = FloatPart.TrimEnd('0');
if (FloatPart =="")
{
FloatPart = "0";
}

if (DecimalPart =="0" & FloatPart =="0")
{
textBox1.Text = "0";
IsFloat = false;
}
if (DecimalPart !="0" & FloatPart =="0")
{
textBox1.Text = DecimalPart;
IsFloat = false;
}
if (DecimalPart =="0" & FloatPart !="0")
{
textBox1.Text = DecimalPart + "," + FloatPart;
IsFloat =true;
}
if (DecimalPart !="0" & FloatPart !="0")
{
textBox1.Text = DecimalPart + "," + FloatPart;
IsFloat = true;
}
}

//
// if it is decimal number
//
else
{
IsFloat = false;
textBox1.Text = textBox1.Text.TrimStart('0');
if (textBox1.Text =="")
{
textBox1.Text = "0";
}
}
textBox1.Refresh();
}

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;
}

//
// Chesk if selected number base to Convert number in is different from 10
// if it is convert to desirable number base
//
if ( ConvertToNumberBase == 10 )
{
textBox2.Text = RealNumber.ToString();
}

if ( EnteredNumberBase == ConvertToNumberBase)
{
textBox2.Text = textBox1.Text;
}
if ( EnteredNumberBase != ConvertToNumberBase & ConvertToNumberBase != 10 )
{
ConvertDecimalToDifferentNumberBase();
}
}

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;
}
}

void ConvertDecimalToDifferentNumberBase()
{
//
// Resolving conversion from Decimal to any number system
//

//
// If it is Float number, split number to Decimal and Float part
// exmpl. 123,345(10) DecimalPart = 123 and FloatPart = 345
//
// It wan't work if the number is in scientific format or have too many digits
//

DecimalFloat = new string[2];
DecimalPart = "";
FloatPart = "";
Change = false;
if ( RealNumber.ToString().Contains(",") == true & RealNumber.ToString().Contains("E") == false)
{
DecimalFloat = RealNumber.ToString().Split(',');
//
// Decimal part first
//
DecimalPart = DecimalFloat[0];
DecimalNumber = double.Parse(DecimalPart);
ConvertedToChosenNumberSistem = "";
Integral = 0;
DivisionResult = 0;
MultiplicationResult = 0;
Cipher = 0;
Letter = "";
while (DecimalNumber != 0 )
{
DivisionResult = DecimalNumber/ ConvertToNumberBase;
Integral = Math.Truncate(DivisionResult);
MultiplicationResult =  ConvertToNumberBase * Integral ;
Cipher = Math.Truncate(DecimalNumber - MultiplicationResult);
DecimalNumber = Integral;
CipherSelect = int.Parse(Cipher.ToString());
SelectValueForLetter();
ConvertedToChosenNumberSistem = ConvertedToChosenNumberSistem.Insert(0,Letter);
}
//
// if DecimalPart number where zero
//
if (ConvertedToChosenNumberSistem == "")
{
ConvertedToChosenNumberSistem = "0";
}
DecimalPart = ConvertedToChosenNumberSistem;

//
// Float part second
//
FloatPart = "0," + DecimalFloat[1];
FloatNumber = double.Parse(FloatPart);
ConvertedToChosenNumberSistem = "";
Integral = 0;
MultiplicationResult = 0;
Cipher = 0;
Letter = "";
Lenght = 0;
while (FloatNumber != 0)
{
MultiplicationResult = FloatNumber * ConvertToNumberBase;
Integral = Math.Truncate(MultiplicationResult);
FloatNumber =  MultiplicationResult - Integral ;
Cipher = Integral;
CipherSelect = int.Parse(Cipher.ToString());
SelectValueForLetter();
ConvertedToChosenNumberSistem = ConvertedToChosenNumberSistem + Letter;
Lenght = ConvertedToChosenNumberSistem.Length;
if ( Lenght > 255 )
{
FloatNumber = 0;
}
}
FloatPart = ConvertedToChosenNumberSistem;

ConvertedToChosenNumberSistem = "";
ConvertedToChosenNumberSistem = DecimalPart + "," + FloatPart;
Change = true;
textBox2.Text =  ConvertedToChosenNumberSistem;
}
else if ( RealNumber.ToString().Contains(",") == false & RealNumber.ToString().Contains("E") == false )
{
//
// If entered number where Integer
//
DecimalPart = RealNumber.ToString();
DecimalNumber = double.Parse(DecimalPart);

ConvertedToChosenNumberSistem = "";
Integral = 0;
DivisionResult = 0;
MultiplicationResult = 0;
Cipher = 0;
Letter = "";
while (DecimalNumber != 0 )
{
DivisionResult = DecimalNumber/ ConvertToNumberBase;
Integral = Math.Truncate(DivisionResult);
MultiplicationResult =  ConvertToNumberBase * Integral ;
Cipher = Math.Truncate(DecimalNumber - MultiplicationResult);
DecimalNumber = Integral;
CipherSelect = int.Parse(Cipher.ToString());
SelectValueForLetter();
ConvertedToChosenNumberSistem = ConvertedToChosenNumberSistem.Insert(0,Letter);
}
//
// if entered number where zero
//
if (ConvertedToChosenNumberSistem == "")
{
ConvertedToChosenNumberSistem = "0";
}
Change = true;
textBox2.Text =  ConvertedToChosenNumberSistem;
}
}

void SelectValueForLetter()
{
switch (CipherSelect)
{
case 0: Letter = "0";
break;
case 1: Letter = "1";
break;
case 2: Letter = "2";
break;
case 3: Letter = "3";
break;
case 4: Letter = "4";
break;
case 5: Letter = "5";
break;
case 6: Letter = "6";
break;
case 7: Letter = "7";
break;
case 8: Letter = "8";
break;
case 9: Letter = "9";
break;
case 10: Letter = "A";
break;
case 11: Letter = "B";
break;
case 12: Letter = "C";
break;
case 13: Letter = "D";
break;
case 14: Letter = "E";
break;
case 15: Letter = "F";
break;
}
}

void TextBox1Enter(object sender, EventArgs e)
{
//
// Set blank value to text box 1
//
textBox1.Text = "";
textBox1.Refresh();
}

void TextBox1MouseEnter(object sender, EventArgs e)
{
//
// show tool tip
//
toolTip1.SetToolTip(textBox1,"     Up to 15 digits ! \n" +
"     Program won't convert number with \n" +
"     scientific number format .");
toolTip1.ToolTipTitle= "Information";
toolTip1.ToolTipIcon = ToolTipIcon.Info;
toolTip1.Active = true;
}

void TextBox2MouseEnter(object sender, EventArgs e)
{
//
// show tool tip
//
textBox2.Refresh();
if ( RealNumber.ToString().Contains("E") )
{
toolTip1.SetToolTip(textBox2,"Converted number have scientific format \n" + textBox2.Text);
toolTip1.ToolTipTitle= "Information";
toolTip1.ToolTipIcon = ToolTipIcon.Info;
toolTip1.Active = true;
}
else
{
toolTip1.SetToolTip(textBox2,"Converted number \n" + textBox2.Text);
toolTip1.ToolTipTitle= "Information";
toolTip1.ToolTipIcon = ToolTipIcon.Info;
toolTip1.Active = true;
}
}

void WriteMessage()
{
//
// Writes message about possible base of the entered number
// and sets the values in the (  ) for further selection
//
MessageBox.Show("\nYou have entered a number \n" +
"with possible number system base\n" +
"in between ( "+ EnteredNumberBaseMax.ToString() +" ) and ( 16 )",
" Information ",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
EnteredNumberBaseMin = EnteredNumberBaseMax;
ConvertToNumberBase = 10;
EnteredNumberBaseMax = 16;
label6.Text = "( " +  EnteredNumberBaseMax + " )";
label7.Text = "( " + ConvertToNumberBase + " )";
label8.Text = "( " + ConvertToNumberBase + " )";
EnteredNumberBase = EnteredNumberBaseMax;
}

{
//
// Writes message about entered number with base ( 16 ) , hexadecimal number
// and sets the values in the (  ) for further selection
//

MessageBox.Show("\nYou have entered a number \n" +
"with number system base ( 16 ).   \n" +
" Information ",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
ConvertToNumberBase = 10;
EnteredNumberBaseMax = 16;
label6.Text = "( " +  EnteredNumberBaseMax + " )";
label7.Text = "( " + ConvertToNumberBase + " )";
label8.Text = "( " + ConvertToNumberBase + " )";
EnteredNumberBase = EnteredNumberBaseMax;
EnteredNumberBaseMin = 16;

}

void WriteErrorMessage()
{
//
// Writes message about entry error
// entry is not number in any of possible number system
//
MessageBox.Show("Warning ! \n"+
"You did not enter a number \n" +
"in any of the predicted number systems !?",
" Entry error ",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
label6.Text = "(    )";
label8.Text = "(    )";
textBox1.Text = "";
textBox1.Focus();
textBox1.Refresh();
}

void WriteE1Message()
{
//
// write warning message
//
toolTip1.SetToolTip(button1,"     Warning ! \n"+
"     Number have too many digits ");
toolTip1.ToolTipTitle= "Information";
toolTip1.ToolTipIcon = ToolTipIcon.Info;
toolTip1.Active = true;
}

void WriteE2Message()
{
//
// write warning message
//
toolTip1.SetToolTip(button2,"     Warning ! \n"+
"     Number have too many digits ");
toolTip1.ToolTipTitle= "Information";
toolTip1.ToolTipIcon = ToolTipIcon.Info;
toolTip1.Active = true;
}

void WriteE3Message()
{
//
// write warning message
//
toolTip1.SetToolTip(button3,"     Warning ! \n"+
"     Number have too many digits ");
toolTip1.ToolTipTitle= "Information";
toolTip1.ToolTipIcon = ToolTipIcon.Info;
toolTip1.Active = true;
}

void WriteE4Message()
{
//
// write warning message
//
toolTip1.SetToolTip(button4,"     Warning ! \n"+
"     Number have too many digits ");
toolTip1.ToolTipTitle= "Information";
toolTip1.ToolTipIcon = ToolTipIcon.Info;
toolTip1.Active = true;
}

}
}

```

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 :

## 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#. Given that the article is in the public domain, I hope that in future this problem is going to get an even better solution especially when it comes to precision of calculations,which means working with a much larger number of decimal places for example, 256 instead of 15, in order to achieve absolute accuracy in conversion itself, which is in theory possible for rational numbers.

## History

Last revision 20.05.2012 Author.

## 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

 Perić Željko Software Developer Serbia Member
Serbia
Smederevo
01.10.2011

personal data:

Name : Željko
Surname : Perić
Country : Serbia
e-mail : periczeljkosmederevo@yahoo.com

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

Votes of 3 or less require a comment

 Search this forum Profile popups    Spacing RelaxedCompactTight   Noise Very HighHighMediumLowVery Low   Layout Open AllThread ViewNo JavascriptPreview   Per page 102550
 FirstPrev Next
 Re: C? Perić_Željko 10 May '12 - 7:50
 Decimal Part Separator chaau 8 May '12 - 17:36
 I can see you have hard-coded comma (,) as a digital part separator. I suggest you using the system defined separator. Sign In·View Thread·Permalink
 Re: Decimal Part Separator Perić_Željko 8 May '12 - 18:45
 Number System Conversion delyk 8 Apr '12 - 6:18