如何使用Atoi函数从字符串转换为int?

问题描述

我的最终目标是根据存储在数组中的32个单位长的二进制数创建8个字符长的十六进制值。为此,我试图将二进制数组分解为8个,4个单位长的字符串,并将每个字符串转换为一个int值,以评估每个字符串的十六进制值并将它们放在一起(如果有任何意义)。

但是当我尝试使用“ binGroup = atoi(str);”​​将字符串转换为int时并打印出来,它只会给我这个:

convert to int: 1011
convert to int: 100
convert to int: 0
convert to int: 0
convert to int: 0
convert to int: 0
convert to int: 0
convert to int: 0

与只打印正确值的直接“ str”相反:

convert to int: 1011
convert to int: 0100
convert to int: 0000
convert to int: 0000
convert to int: 0000
convert to int: 0000
convert to int: 0000
convert to int: 0000

当我将其转换为int值时,为什么不能打印出正确的4 int长二进制数?

这是我的代码,在此先感谢您阅读我冗长的问题。寻找我的解决方案的任何更有效的方法也将不胜感激。

i = 0;
  j = 0;
  int m,binGroup;
  int small[4];
  char str[128];
  while(i < N) {
    for(j = 0; j < 4; j++) {
      small[j] = binRep[i + j];
    }
    int index = 0;
    for(m = 0; m < 4; m++) {
      index += snprintf(&str[index],128 - index,"%d",small[m]);
    }
    binGroup = atoi(str);
    printf("convert to int: %d\n",binGroup);
    i += 4;

    /*for(m = 0; m < 4; m++) {
      charSmall[m] = small[m];
      printf("%d",charSmall[m]);
    }*/

  }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...