将整数数组转换为C中的字符数组
我需要知道如何将整数数组转换为C中的字符数组。 我有1000个元素int阵列,我将有4000个字节大小的字符阵列。 如何将每个整数元素填充到字符数组中,以便在打印时,将所有整数显示在单个字符串中。 示例:dsid_list [3] = {1000,1001,1002}; 那么char_dsid_array应该具有“ 100010011002” 以下是代码段,试图执行相同的代码。
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
int main()
{
uint32_t dsid_count = 1000;
printf("dsid_count:%lu\n",dsid_count);
uint32_t dsid_list[dsid_count];
uint32_t i;
printf("start\n");
for(i=0; i<dsid_count; i++) {
dsid_list[i] = i;
}
printf("Size of dsid_list:%lu\n", sizeof(dsid_list));
//char char_dsid_array[sizeof(dsid_list)];
char *char_dsid_array = (char*)malloc(sizeof(dsid_list) + 1);
i=0;
char* temp = (char*)malloc(4);
char* temp1 = char_dsid_array;
for(i=0; i<dsid_count; i++) {
sprintf(temp, "%lu", dsid_list[i]);
if(i == 0) {
strcpy(char_dsid_array, temp);
}
else {
strcat(char_dsid_array, temp);
}
char_dsid_array = char_dsid_array + 4;
}
char_dsid_array = temp1;
printf("DSID list: %s\n", char_dsid_array);
free(char_dsid_array);
free(temp1);
free(temp);
return 0;
}
I need to know how we can convert integer array to character array in C.
I have 1000 elements int array and I will have 4000 byte sized char array.
How to fill each integer elements into character array so that when printed all integers are shown in single string.
Example: dsid_list[3] = {1000, 1001, 1002};
Then char_dsid_array should have "100010011002"
Below is the code snippet trying to perform same.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
int main()
{
uint32_t dsid_count = 1000;
printf("dsid_count:%lu\n",dsid_count);
uint32_t dsid_list[dsid_count];
uint32_t i;
printf("start\n");
for(i=0; i<dsid_count; i++) {
dsid_list[i] = i;
}
printf("Size of dsid_list:%lu\n", sizeof(dsid_list));
//char char_dsid_array[sizeof(dsid_list)];
char *char_dsid_array = (char*)malloc(sizeof(dsid_list) + 1);
i=0;
char* temp = (char*)malloc(4);
char* temp1 = char_dsid_array;
for(i=0; i<dsid_count; i++) {
sprintf(temp, "%lu", dsid_list[i]);
if(i == 0) {
strcpy(char_dsid_array, temp);
}
else {
strcat(char_dsid_array, temp);
}
char_dsid_array = char_dsid_array + 4;
}
char_dsid_array = temp1;
printf("DSID list: %s\n", char_dsid_array);
free(char_dsid_array);
free(temp1);
free(temp);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无论如何,
uint32_t
可以将值提取到4294967295
(2 32 - 1),因此4
bytes字符空间不足以将该数字存储为字符串。幸运的是,您正在尝试使用范围中的数字来创建字符串
[0..999]
只需循环浏览数字,然后从
sprintf()
使用返回值字符串的后缀号。sprintf()
(&amp;printf()
兄弟姐妹)返回成功写入目标的字符数。dsid
确实大于数字1000
,则需要分配足够的空间,例如10010
(10 x 1001
代码> =10010
)字节,以使千ID在更安全的侧面。realloc()
在必要时修剪未使用的内存,例如:Anyway,
uint32_t
can take values up to4294967295
(232 - 1), so4
bytes character space is not enough for storing that number as a string.Luckily, you're trying to create string using numbers in range
[0..999]
Just loop through the numbers and use return value from
sprintf()
to suffix numbers to the string.sprintf()
(&printf()
siblings) returns number of characters written to the target on success.DSID
s are indeed larger than number1000
, then you need allocate sufficient space like10010
(10 x 1001
=10010
) bytes for thousand such IDs to be on the safer side.realloc()
to trim unused memory if necessary, like:4个字符足以存储0到999之间整数的基本10字符串表示形式,但通常不足以存储任意
int
。您可以使用snprintf
来计算生成字符串表示形式所需的空间数量。我建议在初始化数组,然后分配一次空间时累积所需的大小。例如:4 characters is enough to store the base-10 string representation of integers between 0 and 999, but in general it will no be enough to store an arbitrary
int
. You can usesnprintf
to compute the amount of space you need to generate a string representation. I would suggest accumulating the size you will need as you initialize the array and then allocate space once. eg:先生,下面有案例的代码示例:
请注意一些安全的问题,在为阵列添加新整数之前,我们必须检查是否有足够的空间,如果有足够的空间,我将在consantate之前:
if(sizeof(my_list)&gt;(str_number(str_number)(str_number) ) + strlen(my_list)))
另一个可爱的安全求职者是
snprintf
,此功能改进sprintf
以避免溢出。Here's an online working code
Sir, down below there is code example for the case:
Please note some secure issues, before add a new integer to the array we must check there is enough space, before concantenate I will if there is space enough:
if(sizeof(my_list) > (strlen(str_number) + strlen(my_list)))
Another cute security recourse is
snprintf
, this function improvessprintf
to avoid overflow.Here's an online working code link