Search code examples
ccharmac-addresswiimote

Ouptut PIN from a MAC address using char


I am absolutly not doing C, but for a small thing, I need to make a very simple function. I have problem understanding the result.

I have a MAC address and I need to output the PIN code. (wiimote) I got this function so far

int main(int argc, char *argv[])
{
char pin[6];
pin[0] = 0x61;
pin[1] = 0xC7;
pin[2] = 0x5E;
pin[3] = 0x00;
pin[4] = 0x9E;
pin[5] = 0xCC;
printf(pin);
return 0;
}

Problem is that I get as a result: aヌ^ Is it what I am supposed to get? Value should be different?

The point as David Hoelzer said, I might find a solution converting Hexa to string?

Thank you!


Solution

  • Is this what you need?

    unsigned char pin[6];   // Unsigned ...
    pin[0] = 0x61;
    pin[1] = 0xC7;
    pin[2] = 0x5E;
    pin[3] = 0x00;
    pin[4] = 0x9E;
    pin[5] = 0xCC;
    
    printf("%02x:%02x:%02x:%02x:%02x:%02x\n",pin[0],pin[1],pin[2],pin[3],pin[4],pin[5]);
    

    will print

    61:c7:5e:00:9e:cc
    

    Explanation:

    %x   // Print a number in hexadecimal format using lower case (%X for upper case)
    %2x  // Print at least 2 characters (prepend with space)
    %02x // Print at least 2 characters and prepend with 0
    

    Use unsigned char pin[6] to avoid sign extention during print. If you use char pin[6] you'll get

    61:ffffffc7:5e:00:ffffff9e:ffffffcc
    

    which is probably not what you want.

    If you for some reason need to use char, you can do:

    char pin[6];
    pin[0] = 0x61;
    pin[1] = 0xC7;
    pin[2] = 0x5E;
    pin[3] = 0x00;
    pin[4] = 0x9E;
    pin[5] = 0xCC;
    
    printf("%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",pin[0],pin[1],pin[2],pin[3],pin[4],pin[5]);
               ^^
               Force type to char