gpt4 book ai didi

c - 如何获取特定的数组元素

转载 作者:太空宇宙 更新时间:2023-11-03 23:51:10 25 4
gpt4 key购买 nike

我已经声明了字符数组 char BCommand[100]="00LI002LE99" ,我只想使用 00299 .

这是我试过的代码:

 char Lcommand[3],Lecommand[3];
unsigned char SlaveNodeID=0,NodeValue=0,ChSum2=0;
Lcommand[0]=BCommand[4];
Lcommand[1]=BCommand[5];
Lcommand[2]=BCommand[6];
Lecommand[0]=BCommand[9];
Lecommand[1]=BCommand[10];
SlaveNodeID = atoi(Lcommand);
NodeValue = atoi(Lecommand);

有什么有效的方法吗?

最佳答案

我想你需要 int 值,如果你想要 00299 的整数值,你可以使用它。

int SlaveNodeID = (BCommand[4] - '0') * 100 + (BCommand[5]-'0') * 10 + (BCommand[6] - '0');
int NodeValue = (BCommand[5] - '0') * 10 + (BCommand[6]-'0');

printf("%3d",SlaveNodeID);
printf("%2d",NodeValue);

如果你想把它们变成字符串,然后声明大小为 4 和 3 的字符数组。使用 sprintf()snprintf() 而不是 printf ()

关于c - 如何获取特定的数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19605824/

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