a hexadecimal problem...

ey!! its me again, 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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

#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;
}





I TRIED MY BEST BUT STILL I CAN'T FIGURE IT OUT.. HALPP MEHH!! XD
Do you insist on doing this per hand? Because the streaming facilities of STL (string stream or cin) can do this for you. Instead of thinking about the apprearance of your integer values just store them as ints. If you print them, how ever you like: (octal, decimal and hex are supported)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
	using namespace std;
	int n = 123;

	cout << hex << showbase << n << endl;
	cout << hex << noshowbase << n << endl;

	cout << dec << showbase << n << endl;
	cout << dec << noshowbase << n << endl;

	cout << oct << showbase << n << endl;
	cout << oct << noshowbase << n << endl;

	return 0;
}



If your input provide you with an integer as hexadecimal 'cin' can read it normally with

 
    cin >> n; // read hexadicamal intergers, too 


For sure, we can write a function together which transform your values from one base to a second base. But the question is always how to store something like this. In a char? In an array or a vector?

If you insist on keeping your logic how it is, i can provide you with a solution too, but i doubt if it is really useful to do it like this. Or is it an excercise?

I want to help,

Maikel
Last edited on
Topic archived. No new replies allowed.