Conversion from decimal to binary and vice versa

By | 3 kwietnia 2012

Today little more counting :). Many times, as IT specialist, I needed to convert from decimal number to binary and the other way round, that is why I made presentations in which I want to show you algorithm that I’m using. I don’t know if it is the best way, but I’m sure that this algorithm is effortless for me.

Further I’m giving you codes in C++, so you can convert the numbers automatically on your computers. Purposely I’m not posting the compiled program, because I hope that you already have the appropriate compiler.

Code: Decimal => Binary:

[spoiler show=”Show” hide=”Hide”]

///////////////////////////////////////////
// Decimal to binary based on the stack //
///////////////////////////////////////////
#include <iostream>
#include <conio.h>

class Stos
{
public:
// array containing stack elements
 int dane[100];
// variable storing amount of stack elements
 int n;
// stack initiating
 void init() { n=0; }
// placing elements on the top of the stack
 void push(int e) { dane[n++] = e; }
// taking off the element from the stack
 int pop() { if(n!=0)return dane[--n]; }
// function - checking if stack is empty
 int empty() { return n==0; }
};

using namespace std;

main()
{
// creating object stack in class Stack
 Stos stos;
//initiating object stack
stos.init();

 cout << "Decimal to binary conversion\n\n";
 cout << "Number: ";

// variable 'a' declaration, variable is storing a number
// which will be changed to binary
 int a = 0;
//entering the number to variable
 cin >> a;
// until 'a' is different from 0, 'a' is divided by 2 and the remainder
// goes on the stack, and 'a' is changing on the result of dividing by 2
 while(a!=0) {stos.push(a % 2); a/=2;}
// stack gets only as many bits on which the number can be minimally saved
// that is why I used a loop to always have representation on multiple of 4 number of bites
 for(int i=0; i<stos.n%4; i++) cout << "0";
// writting out bites from stack until is empty
 while(!stos.empty())
 cout << stos.pop();
// stop program execution to see the result
getch();
}

[/spoiler]

Code Binary => Decimal:

[spoiler show=”Show” hide=”Hide”]


///////////////////////////////////////////
// Binary to decimal //
///////////////////////////////////////////
#include <string>
#include <iostream>
#include <conio.h>

using namespace std;

int BinToDec(string num)
{
 int result = 0, pow = 1;
 for ( int i = num.length() - 1; i >= 0; --i, pow <<= 1 )
 result += (num[i] - '0') * pow;

return result;
}
main()
{
cout << "Enter a number in binary notation, it will be converted to decimal\n\n";
cout << "NUmber: ";
string bin;
cin >> bin;
cout << BinToDec(bin);

getch();
}

[/spoiler]

Converting from decimal notation to binary – Presentation 

[docstoc docId=”117809271″ mId=”23309449″ width=”630″ height=”550″ slideMode=”true” showRelatedDocs=”true” showOtherDocs=”true” allowdownload=”true” url=”http://www.docstoc.com/docs/117809271/Dziesietne-do-binarnego”]Decimal to binary[/docstoc]

Converting from binary notation to decimal – Presentation 

[docstoc docId=”117809140″ mId=”23309449″ width=”630″ height=”550″ slideMode=”true” showRelatedDocs=”true” showOtherDocs=”true” allowdownload=”true” url=”http://www.docstoc.com/docs/117809140/Zamiana-z-binarnego-na-dziesiÄ™tny”]Binary to decimal[/docstoc]

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *