Created
November 1, 2010 19:43
-
-
Save anonymous/658733 to your computer and use it in GitHub Desktop.
It converts hexadecimals to binary but it takes out some zeroes. I was told to use strings on the binary but not sure how to use them on binary to get it to output the zeroes it has been cutting off.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
string hexaNumber,hexa,truebinaryNumber; | |
int strlength,i,decimalNumber=0,hx,binary,iterator=0,binaryNumber=0; | |
cout <<"Enter Hexa Number\n"; | |
cin >>hexaNumber; | |
strlength = hexaNumber.length(); | |
for (i=0;i<strlength;i++) | |
{ | |
hexa=hexaNumber.substr(i,1); | |
if (hexa >="0" && hexa <= "9") | |
cout <<"Your number is being converted "<<endl; | |
else | |
{ | |
if (hexa =="a") | |
hexa = "10"; | |
else if (hexa =="b") | |
hexa="11"; | |
else if (hexa =="c") | |
hexa ="12"; | |
else if (hexa =="d") | |
hexa ="13"; | |
else if (hexa =="e") | |
hexa ="14"; | |
else if (hexa =="f") | |
hexa ="15"; | |
else if (hexa =="g") | |
hexa ="16"; | |
else | |
cout <<"Invalid"; | |
} //convert from string "hexa" to int "hex" | |
hx = atoi (hexa.c_str()); | |
decimalNumber += hx * pow(16.0,strlength-i-1); | |
} | |
while (decimalNumber>0) | |
{ | |
binary = decimalNumber%2; | |
binaryNumber += binary * pow(10.0,iterator); | |
decimalNumber/=2; | |
iterator++; | |
} | |
cout << binaryNumber<<endl; | |
system ("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment