I am going to make a program that will convert from hexadecimal string, then to binary and finally to decimal. Now here's the problem (i guess) : when I try to convert hexadecimal string to binary string, I just did a "cout" and this is not a binary string. So, what am I exactly going to do to make a binary string from my hexadecimal string? I don't know what to do, I tried my best for days but still, there's no improvement.. T_T
:((
Code:
#include <iostream>
#include <string.h>
#include <conio.h>
using namespace std;
//this function is for my hexadecimal input, to filter the keys from the keyboard
char* hex (char str1[])
{
int i=0,x=0;
do{
str1[x]=getch();
str1[x]=toupper (str1[x]);
if (((str1[x]>='0') && (str1[x]<='9')) || ((str1[x]>='A') && (str1[x]<='F'))){
cout<<str1[x];
x++;
}
}while (str1[x]!=13);
str1[x]='\0';
return str1;
}
//and this function is for my binary to decimal function.
int bd (char bin[]){
int c,x=0,i,factor=1;
int decimal=0;
int len;
len = strlen (bin)-1;
for (i=0;i<=len;len--){
if (bin[len]==49){
c=bin[len]-48;}
else if (bin[len]==48){
c=bin[len]-48;}
decimal = decimal + c*factor;
factor = factor*2;
}
return decimal;
}
main (void)
{
int i;
char s[40];
hex (s);
//this is for my hexadecimal to binary (the part where I need help T_T)
for(i=0;s[i]!='\0';i++)
{
switch(s[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;
}
}
system ("PAUSE");
return 0;
}
Comment