Home > OS >  How to convert char array to uint8_t and print it in c?
How to convert char array to uint8_t and print it in c?

Time:10-23

I have a decimal number and we have converted it to hex and have stored in a hex array. After that I added a "0x" to this number (in fact char array). Now I want to convert this hex number (in char array) to Uint8_t and then print it. The first question is how this convert can be done and the second one is that in the process of printing, what print format (x, %d, %s , and ...) should be used?

CodePudding user response:

This example converts the hex string to an integer:

#include <stdio.h>
    
int main(void)
{
    int val;
    char str[] = "0x12";
    int res = sscanf(str, "%i", &val);
    printf("res=%d val=%d\n", res, val);
}

Program output

res=1 val=18

The %i format is used in scanf function family to accept input in decimal, octal (with a leading 0) or hex (with leading 0x). If you only want to accept decimal use %d or %u.

CodePudding user response:

Give more details about what you want sir.

  • Related