gpt4 book ai didi

将 char* 转换为 int

转载 作者:行者123 更新时间:2023-11-30 21:12:48 26 4
gpt4 key购买 nike

有没有办法将char *转换为int

我有一个从 UART 接收到的字符串 (string1)。该字符串如下所示:{3600,32,300}:

char t1_s[32],t2_s[32],t3_s[32];

static char string1[15];

strcpy(t3_s, strtok(string1 , ","));
strcpy(t2_s, strtok(NULL, ","));
strcpy(t1_s, strtok(NULL, ","));

t1= t1_s - '0';
t2= t2_s - '0';
t3= t3_s - '0';

编译器给出警告:

#515-D a value of type "char *" cannot be assigned to an entity of type "int" main.c

我需要获取 t1_st2_st3_s 的整数值。

最佳答案

使用strtol将字符串转换为整数:

char *s = "3600";
char *error = NULL;
int n = strtol(s, &error, 10);
if (*error != '\0') {
// handle error
}

关于将 char* 转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36774337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com