C语言 将 int 数组转换为 char 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4712737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert int array to char array
提问by CrazyCoder
I am having an array of integer say int example[5] = {1,2,3,4,5}. Now I want to convert them into character array using C, not C++. How can I do it?
我有一个整数数组 say int example[5] = {1,2,3,4,5}。现在我想使用 C 而不是 C++ 将它们转换为字符数组。我该怎么做?
采纳答案by JeremyP
Depending on what you really want, there are several possible answers to this question:
根据您真正想要什么,这个问题有几个可能的答案:
int example[5] = {1,2,3,4,5};
char output[5];
int i;
Straight copy giving ASCII control characters 1 - 5
直接复制给出 ASCII 控制字符 1 - 5
for (i = 0 ; i < 5 ; ++i)
{
output[i] = example[i];
}
characters '1' - '5'
字符 '1' - '5'
for (i = 0 ; i < 5 ; ++i)
{
output[i] = example[i] + '0';
}
strings representing 1 - 5.
代表 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);
}
回答by karlphillip
#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;
}
Outputs:
输出:
c[0] = 65
c[1] = 66
c[2] = 67
c[3] = 68
c[4] = 69
回答by unwind
You can convert a single digit-integer into the corresponding character using this expression:
您可以使用以下表达式将单个数字整数转换为相应的字符:
int intDigit = 3;
char charDigit = '0' + intDigit; /* Sets charDigit to the character '3'. */
Note that this is only valid, of course, for single digits. Extrapolating the above to work against arrays should be straight-forward.
请注意,这当然仅对个位数有效。将上述内容外推到数组上应该是直截了当的。
回答by peoro
You need to create the array, because sizeof(int)is (almost surely) different from sizeof(char)==1.
您需要创建数组,因为sizeof(int)(几乎肯定)不同于sizeof(char)==1.
Have a loop in which you do char_example[i] = example[i].
有一个你做的循环char_example[i] = example[i]。
If what you want is to convertan integer into a string you could just sum your integer to '0'but only if you're sure that your integer is between 0 and 9, otherwise you'll need to use some more sophisticated like sprintf.
如果您想要将整数转换为字符串,您可以将整数求和,'0'但前提是您确定整数在 0 到 9 之间,否则您需要使用更复杂的sprintf.
回答by Michael Smith
In pure C I would do it like this:
在纯 CI 中会这样做:
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);
}

