【问题标题】:Convert int array to char array将 int 数组转换为 char 数组
【发布时间】:2011-01-17 11:43:56
【问题描述】:

我有一个整数数组,比如int example[5] = {1,2,3,4,5}。现在我想使用 C 而不是 C++ 将它们转换为字符数组。我该怎么做?

【问题讨论】:

  • 1,2,3,4,5 代表什么?您期待哪些合理的转换?请在您的问题中添加一个示例。
  • 这是一个错字...它的 int 示例[5]
  • @winbros - char 数组,您希望它为 {'1','2','3','4','5'} ?
  • 你想处理 1,2...的 ASCII 吗?

标签: c


【解决方案1】:

根据你真正想要的,这个问题有几个可能的答案:

int example[5] = {1,2,3,4,5};
char output[5];
int i;

直接复制给 ASCII 控制字符 1 - 5

for (i = 0 ; i < 5 ; ++i)
{
    output[i] = example[i];
}

字符 '1' - '5'

for (i = 0 ; i < 5 ; ++i)
{
    output[i] = example[i] + '0';
}

代表 1 - 5 的字符串。

char stringBuffer[20]; // Needs to be more than big enough to hold all the digits of an int
char* outputStrings[5];

for (i = 0 ; i < 5 ; ++i)
{
    snprintf(stringBuffer, 20, "%d", example[i]);
    // check for overrun omitted
    outputStrings[i] = strdup(stringBuffer);
}

【讨论】:

  • 整数大于9怎么办? 10 以上的整数不会转换为 char 格式。
【解决方案2】:
#include <stdio.h>

int main(void)
{
    int i_array[5] = { 65, 66, 67, 68, 69 };
    char* c_array[5];

    int i = 0;
    for (i; i < 5; i++)
    {   
        //c[i] = itoa(array[i]);    /* Windows */

        /* Linux */
        // allocate a big enough char to store an int (which is 4bytes, depending on your platform)
        char c[sizeof(int)];    

        // copy int to char
        snprintf(c, sizeof(int), "%d", i_array[i]); //copy those 4bytes

        // allocate enough space on char* array to store this result
        c_array[i] = malloc(sizeof(c)); 
        strcpy(c_array[i], c); // copy to the array of results

        printf("c[%d] = %s\n", i, c_array[i]); //print it
    }   

    // loop again and release memory: free(c_array[i])

    return 0;
}

输出:

c[0] = 65
c[1] = 66
c[2] = 67
c[3] = 68
c[4] = 69

【讨论】:

  • 您的答案仅适用于最多 999 的数字,之后它将开始截断数字 - 32 位 2 的补码整数的 INT_MAX 是 2147483647,而不是超过 3 位(您的四个数字之一拍摄于'\0')
【解决方案3】:

您可以使用此表达式将单个数字整数转换为相应的字符:

int  intDigit = 3;
char charDigit = '0' + intDigit;  /* Sets charDigit to the character '3'. */

请注意,这当然只对单个数字有效。推断以上内容以针对数组工作应该是直截了当的。

【讨论】:

    【解决方案4】:

    您需要创建数组,因为sizeof(int)(几乎可以肯定)不同于sizeof(char)==1

    有一个循环,你可以在其中执行char_example[i] = example[i]


    如果你想要convert一个整数到一个字符串,你可以将你的整数与'0'相加,但前提是你确定你的整数在0到9之间,否则你'将需要使用一些更复杂的东西,比如sprintf

    【讨论】:

      【解决方案5】:

      在纯 C 中我会这样做:

      char** makeStrArr(const int* vals, const int nelems)
      {
          char** strarr = (char**)malloc(sizeof(char*) * nelems);
          int i;
          char buf[128];
      
          for (i = 0; i < nelems; i++)
          {
              strarr[i] = (char*)malloc(sprintf(buf, "%d", vals[i]) + 1);
              strcpy(strarr[i], buf);
          }
          return strarr;
      }
      
      void freeStrArr(char** strarr, int nelems)
      {
          int i = 0;
          for (i = 0; i < nelems; i++) {
              free(strarr[i]);
          }
          free(strarr);
      }
      
      void iarrtostrarrinc()
      {
          int i_array[] = { 65, 66, 67, 68, 69 };
          char** strarr = makeStrArr(i_array, 5);
          int i;
          for (i = 0; i < 5; i++) {
              printf("%s\n", strarr[i]);
          }
          freeStrArr(strarr, 5);
      }
      

      【讨论】:

      • malloc-cast 不是“纯 C”,而是它的 C++。
      猜你喜欢
      • 2011-11-09
      • 1970-01-01
      • 2014-03-02
      • 2013-12-11
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多