简体   繁体   中英

How to convert an ASCII string into a decimal number?

I am trying to convert an ASCII string into a decimal number, but it doesn't work I tried this way.

char tab[4] = {53,70,51,68};
int a = (int)strtol(tab, NULL, 16);
printf("a = %d",a);

as input i have 53,70,51,68 => in hex 5F3D as output i should got => 24381

Your char array is missing the NUL terminator ( 0 == 0x00 == '\0' ).

const char tab[5] = {53, 70, 51, 68, 0};
               
int a = (int) strtol(tab, NULL, 16);

printf("number: %d\n", a);

Source: http://www.cplusplus.com/reference/cstdlib/strtol/

It might work if it is missing, but this is undefined behavior, see comments below.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM