How to convert char to hex stored in uint8_t form?

后端 未结 5 1009
星月不相逢
星月不相逢 2021-01-27 05:11

Suppose I have these variables,

const uint8_t ndef_default_msg[33] = {
    0xd1, 0x02, 0x1c, 0x53, 0x70, 0x91, 0x01, 0x09,
    0x54, 0x02, 0x65, 0x6e, 0x4c, 0x69         


        
5条回答
  •  别那么骄傲
    2021-01-27 05:23

    Why not read it into ndef_msg directly, (minus the \0 if it suppose to be a pure array). The hex are just for presentation, you could have just picked decimal or octal with no consequence for the content.

    void print_hex(uint8_t *s, size_t len) {
        for(int i = 0; i < len; i++) {
            printf("0x%02x, ", s[i]);
        }
        printf("\n");
    }
    
    int main()
    {
        uint8_t ndef_msg[34] = {0};
    
        scanf("%33s", ndef_msg);
        print_hex(ndef_msg, strlen((char*)ndef_msg));
    
    
        return 0;
    }
    

    You probably need to handle the reading of the string differently to allow for whitespace and perhaps ignore \0, this is just to illustrate my point.

提交回复
热议问题