简体   繁体   English

如何将 ASCII 字符串转换为十进制数?

[英]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.我正在尝试将 ASCII 字符串转换为十进制数,但我尝试过这种方式不起作用。

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作为输入,我有 53,70,51,68 => 十六进制 5F3D 作为 output 我应该得到 => 24381

Your char array is missing the NUL terminator ( 0 == 0x00 == '\0' ).您的 char 数组缺少 NUL 终止符( 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/资料来源: http://www.cplusplus.com/reference/cstdlib/strtol/

It might work if it is missing, but this is undefined behavior, see comments below.如果缺少它可能会起作用,但这是未定义的行为,请参阅下面的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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