MAC Address printing

后端 未结 4 653
北海茫月
北海茫月 2021-01-12 10:06

This is a code that gets some info about network the problem is when it prints the MAC address it prints it sometime normally and sometime with fff\'s like 00:21:84:a2:12:88

4条回答
  •  北恋
    北恋 (楼主)
    2021-01-12 10:29

    I prefer use an explicit length modifier in format string for values shorter than int. For example %02hhx instead of %02x for one-byte values. It allow me to not worry about those subtle conversion and promotion issues:

    #include 
    
    int main(void)
    {
        signed char sff = '\xff';
        unsigned char uff = '\xff';
    
        printf("signed:   %02x %02hhx\n", sff, sff);
        printf("unsigned: %02x %02hhx\n", uff, uff);
    
        return 0;
    }
    

    prints

    signed:   ffffffff ff
    unsigned: ff ff
    

提交回复
热议问题