Overview
" If we ever encounter to inteligent life form from outer space,
that uses positional number system in it's own mathematics,
we shall need something to quickly and accurately translate it
to number system that is understandable to us. " - Author
Program for conversion of numbers from one to another positional number system was written as "Windows Form Application”, in Sharp Develop IDE , programming language C# ver. 4.0, and for it's work is essential Microsoft .Net framework ver. 4.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 to decimal number system, entered 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.
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 open source project
If you just want to use program you can download it here. After downloading just click on executable file. Important , program needs Microsoft .Net framework 4.0 or higher to work correctly.
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 positional 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 positional number system.
Searching all over the internet for information on how to convert number from one to another positional 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(16) = 8*163 + 15*162 + 14*161 + 10*160 + 12*16-1 + 13*16-2 + 7*16-3= 36842,8024902344(10)
Here we can see the general formula for converting numbers with number system base X to decimal number system :
(a(n)a(n-1)a(n-2)...a(1)a(0), c(1)c(2)c(3)...c(m-2)c(m-1)c(m))(x) =
= ( Sum( a(k) * x(+k) )(k=n down to k=0) + Sum( c(j) * x(-j) )(j=1 to j=m) )(10)
a – value of digit inside Integral part of number
represented as number value from decimal number system
n – position of digit inside Integral part of number
c - value of digit inside Fractal part of number
represented as number value from decimal number system
m - position of digit inside Fractal part of number
x – system base value of selected number for conversion
k – counter of digit position inside Integral part of number
j – counter of digit position inside Fractal part of number
Notice :
The result of conversion to decimal number sistem is exact only if number is rational, otherwise it is approximate because of rounding to the desirable number of decimal places. There is limit of size and precision of numbers. Since this program uses 'Double' type of variables for storing values of floating point numbers, it's precision is up to 15-16 digits and it's approximate range is from ±5.0 × 10−324 to ±1.7 × 10+308 . On this subject should be more exploration work , with final goal that program works with absolute precision. Now program is precise up to fifteen digits when converting number from other to decimal system. And there is problem with scientific format of representing numbers, which is automatic in some programming languages and do not enables to user to see all digits in converted number !?
-
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 sistem of the entered number)
- Assign value for K ( K = N, goes from N down to 0, position of digit inside number)
-
Assign value for A ( A = value of digit in Integral part of number at K position, represented as number value from decimal number system )
- 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 J ( J = 1 , goes from 1 to M, position of digit inside number)
-
Assign value for C ( C = digit in Fractal part of number at K position, represented as number value from decimal number system )
- Sum = Sum + C( j ) * X( -j )
- Increase value of J by one ( J = J + 1 )
- If J is less then M+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 Integral and Result and remember result 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 (condition for stopping process of conversion)
-
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 part * Base
A * B
|
Integral part of result of multiplication
A * B
|
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
|
-
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 (condition for stopping process of conversion)
-
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 positional 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 positional 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.
Step two
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 positional 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.
<span lang="EN"><span lang="EN"><span lang="EN">
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace From_one_to_another_positional_number_system
{
public partial class MainForm : Form
{
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 = "";
string NumberDecimalSeparator = "";
string NumberGroupSeparator = "";
string NumberDecimalDigits = "";
const double exmpl = 1234567890.12345;
bool Change = false;
public MainForm()
{
InitializeComponent();
NumberDecimalSeparator = Application.CurrentCulture.NumberFormat.NumberDecimalSeparator;
NumberGroupSeparator = Application.CurrentCulture.NumberFormat.NumberGroupSeparator;
NumberDecimalDigits = Application.CurrentCulture.NumberFormat.NumberDecimalDigits.ToString();
MessageBox.Show( "Number to convert can be entered \n" +
"in default or intern number format. \n" +
"In any way, output shall be in intern number format. \n\n" +
"Current culture number format : \n\n" +
" " + exmpl.ToString("N") + " \n\n" +
"Number decimal separator : " + NumberDecimalSeparator + " \n" +
"Number group separator : " + NumberGroupSeparator + " \n" +
"Number of decimal digits : " + NumberDecimalDigits + " \n\n" +
"Intern number format : \n\n" +
" " + exmpl.ToString() + " \n\n" +
"Number decimal separator : " + NumberDecimalSeparator.ToString() + " \n" +
"Number group separator : none \n" +
"Number of decimal digits : 0 - 254" ,
" Number Format Information ",
MessageBoxButtons.OK,
MessageBoxIcon.Information );
}
void TextBox1Enter(object sender, EventArgs e)
{
textBox1.Text = "";
textBox1.Refresh();
}
void TextBox1MouseEnter(object sender, EventArgs e)
{
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 TextBox1KeyPress(object sender, KeyPressEventArgs e)
{
if ( e.KeyChar == (char) Keys.Return )
{
label8.Focus();
}
}
void TextBox1Leave(object sender, EventArgs e)
{
string Text = "";
string Letter = "";
string CipherDigits = "1234567890ABCDEF" + NumberDecimalSeparator;
int TextLenght = 0;
int LetterCounter = 0;
int CommaCounter = 0;
EnteredNumberBaseMax = 0;
EnteredNumberBaseMin = 2;
if ( textBox1.Text == "" )
{
textBox1.Text = "2011" + NumberDecimalSeparator + "2013";
}
textBox1.Text = textBox1.Text.ToUpper();
textBox1.Text = textBox1.Text.Replace( " " , "" );
textBox1.Text = textBox1.Text.Replace( NumberGroupSeparator , "");
ClearZeroFromNumber();
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;
}
if( Letter == NumberDecimalSeparator )
{
CommaCounter = CommaCounter + 1;
}
LetterCounter = LetterCounter + 1;
}
else
{
EntryIsNumber = false;
LetterCounter = TextLenght;
}
}
if ( CommaCounter > 1 | EnteredNumberBaseMax == 0 | EntryIsNumber == false )
{
WriteErrorMessage();
}
else
{
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 :
{
WriteHexadecimalMessage();
ConvertToDecimal();
}
break;
}
}
}
void ClearZeroFromNumber()
{
IsFloat = false;
if ( textBox1.Text.Contains( NumberDecimalSeparator ) )
{
IsFloat = true;
DecimalFloat = textBox1.Text.Split( NumberDecimalSeparator[0] );
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 + NumberDecimalSeparator + FloatPart;
IsFloat =true;
}
if ( DecimalPart != "0" & FloatPart != "0" )
{
textBox1.Text = DecimalPart + NumberDecimalSeparator + FloatPart;
IsFloat = true;
}
}
else
{
IsFloat = false;
textBox1.Text = textBox1.Text.TrimStart( '0' );
if ( textBox1.Text == "" )
{
textBox1.Text = "0";
}
}
textBox1.Refresh();
}
void Button1Click(object sender, EventArgs e)
{
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)
{
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)
{
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)
{
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 ConvertToDecimal()
{
Change = false;
if ( IsFloat == true )
{
DecimalPart = "";
FloatPart = "";
RealNumber = 0;
DecimalFloat = textBox1.Text.Split( NumberDecimalSeparator[0] );
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 = Math.Pow( EnteredNumberBase, Exponent );
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 = Math.Pow( EnteredNumberBase, -Exponent );
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 = Math.Pow( EnteredNumberBase, Exponent );
D = D + ( Cipher * BaseExponent );
}
RealNumber = D;
Change = true;
}
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()
{
DecimalFloat = new string[2];
DecimalPart = "";
FloatPart = "";
Change = false;
if ( RealNumber.ToString().Contains( NumberDecimalSeparator ) == true &
RealNumber.ToString().Contains( "E" ) == false )
{
DecimalFloat = RealNumber.ToString().Split( NumberDecimalSeparator[0] );
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 ( ConvertedToChosenNumberSistem == "" )
{
ConvertedToChosenNumberSistem = "0";
}
DecimalPart = ConvertedToChosenNumberSistem;
FloatPart = "0"+ NumberDecimalSeparator + 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 + NumberDecimalSeparator + FloatPart;
Change = true;
textBox2.Text = ConvertedToChosenNumberSistem;
}
else if ( RealNumber.ToString().Contains( NumberDecimalSeparator ) == false &
RealNumber.ToString().Contains( "E" ) == false )
{
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 ( 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 TextBox2MouseEnter(object sender, EventArgs e)
{
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()
{
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;
}
void WriteHexadecimalMessage()
{
MessageBox.Show( "\nYou have entered a number \n" +
"with number system base ( 16 ). \n" +
"It is Hexadecimal number.",
" Information ",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
ConvertToNumberBase = 10;
EnteredNumberBaseMax = 16;
label6.Text = "( " + EnteredNumberBaseMax + " )";
label7.Text = "( " + ConvertToNumberBase + " )";
label8.Text = "( " + ConvertToNumberBase + " )";
EnteredNumberBase = EnteredNumberBaseMax;
EnteredNumberBaseMin = 16;
}
void WriteErrorMessage()
{
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()
{
toolTip1.SetToolTip( button1,
" Warning ! \n" +
" Number have too many digits "
);
toolTip1.ToolTipTitle = "Information";
toolTip1.ToolTipIcon = ToolTipIcon.Info;
toolTip1.Active = true;
}
void WriteE2Message()
{
toolTip1.SetToolTip( button2,
" Warning ! \n"+
" Number have too many digits "
);
toolTip1.ToolTipTitle = "Information";
toolTip1.ToolTipIcon = ToolTipIcon.Info;
toolTip1.Active = true;
}
void WriteE3Message()
{
toolTip1.SetToolTip( button3,
" Warning ! \n" +
" Number have too many digits "
);
toolTip1.ToolTipTitle = "Information";
toolTip1.ToolTipIcon = ToolTipIcon.Info;
toolTip1.Active = true;
}
void WriteE4Message()
{
toolTip1.SetToolTip( button4,
" Warning ! \n" +
" Number have too many digits "
);
toolTip1.ToolTipTitle = "Information";
toolTip1.ToolTipIcon = ToolTipIcon.Info;
toolTip1.Active = true;
}
}
}
</span>
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#. 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 07.06.2016 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.
All about the author you can find at this adress.
Home page