-
Little Unsure?
I have finally found a code that converts decimal to binary. But however I hardly understand it and I think it would be better if I know how to do it for myself instead of using pre fabricated code all the time eh?
I could adapt this code into mine if I understood it completely but I don't so I am goin to post it and hope for sum help. Thanks in advance.
Code:
#include <iostream.h>
#include <stdlib.h>
int a,b;
int n=0;
int arr[100];
void main()
{
cout <<"Decimal : ";
cin >>a;
while (a>0)//just to be sure a is a number
{
n++;//add one to your array varibale I would have guessed you did this last.
b=a/2; //divide the decimal by 2 and assign it to b
if (a%2==0)//if that divsion = 0
{
arr[n]=0; //assign 0 to your array variable
}
else
{
arr[n]=a-(2*b); //if it doesn't = 0 subtract the awnser of 2 times b
} // from the initial decimal not sure why???????????????????
a=b;//unsure why this is here
}
cout<<"Binary : ";// this section out puts the binary backwards????????????????
for (n=n;n>0;n--)
{
cout <<arr[n];
}
system("PAUSE");
}
I have commented the main places I don't understand.
-
This code is a mess, and in some places wrong.
Look back through the threads c-programming threads maybe up to a year. One of the old, ultra-knowledgeable posters, sayah, posted complete sourcecode to do this, as well as other things.
-
The fact that Sayeh and * have the same IP address in no way detracts from the glowing endorsment :p
-
As if we didn't know * was Sayeh after a post or two anyway. ;)
But I do agree with the ultra-knowledgeable part.
-Prelude
-
>As if we didn't know * was Sayeh after a post or two anyway.
hehe, I was wondering if anyone else had been thinking this same thing.
-
I personally like the "BitWise" way, though ultra-knowledgeable part of sayah (or *) is cool! ;)
-
Searched
I searched the forums for ultra-knowledgable and for binary conversion and never found any of the posts u were talking about
-
another way
Depending on if you care about the math or the final answer....
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
int main()
{
stringstream Stream;
char CharRep[32];
int IntRep;
cout<<"Decimal: ";
cin>>CharRep; // Read in as a character array
// convert char[] to int: in c use strtol()
Stream<<CharRep;
Stream>>IntRep;
// because I dont want to bother figuring out why
// setiosflags(ios::hex) doesnt work with stringstream
//
sprintf(CharRep, "%x", IntRep);
// Output it, using the cheat that hex maps to binary
// very easily
for ( int i = 0; i < 32; i++ )
{
switch ( CharRep[i] )
{
case '0': cout<<"0000";break;
case '1': cout<<"0001";break;
case '2': cout<<"0010";break;
case '3': cout<<"0011";break;
case '4': cout<<"0100";break;
case '5': cout<<"0101";break;
case '6': cout<<"0110";break;
case '7': cout<<"0111";break;
case '8': cout<<"1000";break;
case '9': cout<<"1001";break;
case 'a': cout<<"1010";break;
case 'b': cout<<"1011";break;
case 'c': cout<<"1100";break;
case 'd': cout<<"1101";break;
case 'e': cout<<"1110";break;
case 'f': cout<<"1111";break;
case '\0':cout<<"b"<<endl; exit(0);
default:
cout<<" Error!!!"<<endl;
exit(1);
}
}
return 0;
}
-
from the looks of things, you might want to learn how to use arrays, how to declare them, assign values, how to access each element.
then, you might also want to learn how to use pointers because pointers and arrays are very closely related.
and when you done with those ... you could consider taking a look at struct(structures).
the suggestions can go on... but you can start with arrays
all the best