#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Code working for Hex, but not for Binary


    Hi,

    I have two functions. One converts ascii to hex and the other uses the function and displays the inverted data. I have shown the two functions below

    Code:
    /*----------------------*/ 
    /* Convert ASCII to hex */
    /*----------------------*/
    unsigned int asc2hex(char *str)
     {
     unsigned int res;
     char *p;
     res = 0;
     for (p = str; *p != '\0' ; ++p)
     { 
        res <<= 4;
        if (*p >= '0' && *p <= '9')
            res |= *p - '0';
       else
            res |= *p - 'A' + 10;
     }
     return res; 
    }
    Code:
    /*----------------------*/ 
    /* Invert data */ 
    /*----------------------*/ 
    invdata( int cycle, char *actstr, char * userData) 
    { 
    char this_input_data[1024]; 
    unsigned int val; 
    get_facvalue(userData, cycle, this_input_data); 
    val = asc2hex(this_input_data); 
    sprintf(actstr,"%08u",~val); 
    return 0; 
    }
    The above functions work perfectly well. But, when I try to do the same and display the result in binary I don't get the proper result. The result just shows "b". I have shown the two functions below

    Code:
    /*----------------------*/ 
    /* Convert ascii to binary*/
    /*----------------------*/ 
    unsigned int asc2bin(char *str) 
    { 
    unsigned int res; 
    unsigned int binres; 
    char *p; 
    binres = 0; 
    for (p = str; *p != '\0' ; ++p) 
    { 
    if (*p >= '0' && *p <= '9') 
    res = *p - '0'; 
    else 
    res = *p - 'A' + 10; 
    binres <<= 4; 
    switch(res) 
    { 
    case '0': binres|='0000';break; case '1': binres|='0001';break;     case '2': binres|='0010';break; case '3': binres|='0011';break; case '4': binres|='0100';break; case '5': binres|='0101';break; case '6': binres|='0110';break; case '7': binres|='0111';break; case '8': binres|='1000';break; case '9': binres|='1001';break; case 'A': binres|='1010';break; case 'B': binres|='1011';break; case 'C': binres|='1100';break; case 'D': binres|='1101';break; case 'E': binres|='1110';break; case 'F': binres|='1111';break; case 'a': binres|='1010';break; case 'b': binres|='1011';break; case 'c': binres|='1100';break; case 'd': binres|='1101';break; case 'e': binres|='1110';break; case 'f': binres|='1111';break; default:break; 
    } 
    } 
    return binres; 
    }
    Code:
    /*----------------------*/ 
    /* Invert data */ 
    /*----------------------*/ 
    invdata( int cycle, char *actstr, char * userData) 
    { 
    char this_input_data[1024]; 
    unsigned int val; 
    get_facvalue(userData, cycle, this_input_data); 
    val = asc2bin(this_input_data); 
    sprintf(actstr,"%032b",~val); 
    return 0; 
    }
    Can you tell me what is wrong?

    Thanks!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    There is no "%b" format specifier. Didn't you ever read the help file or man page on printf?

    Also, why do you think that "%u" worked for hex? That would output an unsigned decimal, not hexadecimal. You need an x for hex. Again, Read the Manual! (RTFM)

    And just why do you refuse to format your code? The entire idea of code tags is to preserve your code's formatting, so you refusing to format your code you are defeating the purpose of code tags. And your formatting of the cases of a switch statement far beyond lame (you very nearly drove me to use a very non-politically-correct term to describe it). This is how it should have looked:

    Code:
    /*----------------------*/ 
    /* Convert ascii to binary*/
    /*----------------------*/ 
    unsigned int asc2bin(char *str) 
    { 
        unsigned int res; 
        unsigned int binres; 
        char *p; 
        binres = 0; 
        
        for (p = str; *p != '\0' ; ++p) 
        { 
            if (*p >= '0' && *p <= '9') 
                res = *p - '0'; 
            else 
                res = *p - 'A' + 10; 
            binres <<= 4; 
            switch(res) 
            { 
                case '0': binres|='0000'; break; 
                case '1': binres|='0001'; break;     
                case '2': binres|='0010'; break; 
                case '3': binres|='0011'; break; 
                case '4': binres|='0100'; break; 
                case '5': binres|='0101'; break; 
                case '6': binres|='0110'; break; 
                case '7': binres|='0111'; break; 
                case '8': binres|='1000'; break; 
                case '9': binres|='1001'; break; 
                case 'A': binres|='1010'; break; 
                case 'B': binres|='1011'; break; 
                case 'C': binres|='1100'; break; 
                case 'D': binres|='1101'; break; 
                case 'E': binres|='1110'; break; 
                case 'F': binres|='1111'; break; 
                case 'a': binres|='1010'; break; 
                case 'b': binres|='1011'; break; 
                case 'c': binres|='1100'; break; 
                case 'd': binres|='1101'; break; 
                case 'e': binres|='1110'; break; 
                case 'f': binres|='1111'; break; 
                default:
                    break; 
            } 
        } 
        return binres; 
    }
    The entire idea of formatting your code and using code tags to preserve that formatting is so that it will be readable. Not using code tags removes that readability. Not even formatting makes it unreadable from the start. Pulling the stupid crap you did with the switch cases goes far beyond that and smacks of a deliberate affront.

    Please note that while the code for each case should be broken out into individual lines that are properly indented, this is one of the few cases where keeping the entire case on one line actually improves readability.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0
    My code is formatted. When I tried to copy and paste is between the code tags it all got pasted on one single line. I tried to reformat it. This is the first time I'm posting something on a forum, so, I'm obviously not going to be perfect at it. Anyway, its pretty clear you were really annoyed so my apologies.

    I didn't use %u for the hex value. I was just trying different formats. Forgot to change it back to %x before I posted here.

    Thank you for letting me know the %b was my mistake!

IMN logo majestic logo threadwatch logo seochat tools logo