Typecasting hex as char without changing the value in C

143 Views Asked by At
void set_binuid(uint8_t byte, uint8_t i) {
    binuid[(i*4)]   = (byte >> 3) & 0x01;
    binuid[(i*4)+1] = (byte >> 2) & 0x01;
    binuid[(i*4)+2] = (byte >> 1) & 0x01;
    binuid[(i*4)+3] = (byte >> 0) & 0x01;
}
    
    
void hex_to_bin(void) {
    uint8_t i;
    for(i = 0; i < 8; i++) {
        if (wieganduid[i] >= '0' && wieganduid[i] <= '9') {
            /* Numerical representation */
            set_binuid(wieganduid[i] - '0', i);
        } else {
            /* Number represented by a letter */
            switch(wieganduid[i]) { 
                    case 'A':
                            set_binuid(0x0A, i);
                            break; 
                    case 'B': 
                            set_binuid(0x0B, i);
                            break; 
                    case 'C': 
                            set_binuid(0x0C, i);
                            break; 
                    case 'D': 
                            set_binuid(0x0D, i);
                            break; 
                    case 'E': 
                            set_binuid(0x0E, i);
                            break; 
                    case 'F': 
                            set_binuid(0x0F, i);
                            break; 
            }
        }
    }
} 

I am passing the value in these to functions to make binary array out of them, it workes fine as long as uint8_t contained only one hex number or it still works when the value taken in ascii table matches 1-f, but when I get values like already mentioned EF - which converts to ascii i there is obviously no case for i, so Im need to take the EF and move it in another uint8_t with different hex value that would be EF in ascii for the function to work and give me bytes of binary 4 for E and 4 for F.

0

There are 0 best solutions below