Converting hexadecimal to binary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • codemunkee
    New Member
    • Mar 2010
    • 1

    Converting hexadecimal to binary

    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;
    }
    :((
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Presumably by a binary string you mean a char string with only 1 and 0 characters.

    That being the case, just use a string object.

    Your cout << "0101" would become:

    Code:
    string str;
    //etc...
    
    str += "0101";

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      Converting hexadecimal to binary is really quite simple:
      Just convert each hexadecimal character to its four digit binary equivalent.
      so 0xff = 1111 1111
      or 0x27b=0010 0111 1011 //I have left spaces so that it easier to read
      so write the code that will accomplish this and half the problem is solved

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Hexadecimal digits can be upper or lower case. That is, you should for example check for both 'A' and 'a'. Alternatively, you could switch on toupper(s[i]).

        Comment

        Working...