简体   繁体   English

在char数组中存储多个整数

[英]Storing multiple integers in a char array

I've been looking online for a few hours to find this, but haven't found anything yet that's exactly what I need. 我已经在网上寻找了几个小时才能找到它,但还没有找到我真正需要的东西。 I have multiple integers that I need to put into a char* (separated by a space) to be later written to a .txt file. 我需要将多个整数放入char* (用空格分隔)中,然后再写入.txt文件中。 My best attempt until now has been this: 到目前为止,我最大的尝试是:

char* temp = (char)input.Z_pos_Camera_Temperature;

Where input.Z_pos_Camera_Temperature is a member of a struct. 其中input.Z_pos_Camera_Temperature是结构的成员。 I tried 我试过了

char* temp = (char)input.Z_pos_Camera_Temperature + ' ' + input.Z_neg_Camera_Temperature;

but that only added the values of the three chars individually. 但这只会分别添加三个字符的值。 Can someone help me figure this out? 有人可以帮我解决这个问题吗?

In C, you cannot concatenate characters into a string using the + operator like you can in higher level languages, nor can you concatenate several strings into a larger, separate string using the + operator. 在C语言中,不能像在高级语言中那样使用+运算符将字符连接成字符串,也不能使用+运算符将多个字符串连接成较大的单独字符串。

You can, however, use the function sprintf to build a string, like so: 但是,您可以使用sprintf函数来构建字符串,如下所示:

char buffer[100]; // adjust per your needs
sprintf(buffer, "%d %d", input.Z_pos_Camera_Temperature, input.Z_neg_Camera_Temperature);

您可能要使用snprintf。

char buf[32]; snprintf(buf, 32, "%d %d", input.Z_pos_Camera_Temperature, input.Z_neg_Camera_Temperature);

This isn't really intended as an answer because you have already got that. 这并不是真正的答案,因为您已经知道了。 I'm posting to help you understand what you tried actually does. 我要发布邮件是为了帮助您了解您实际尝试的操作。 Looking at the first sample: 看第一个样本:

char* temp = (char)input.Z_pos_Camera_Temperature;

First of all, you should have gotten a warning when you compiled this line. 首先,编译此行时应该已经得到警告。 Something like: 就像是:

warning C4047: 'initializing': 'char *' differs in levels of indirection from 'char' 警告C4047:“正在初始化”:“ char *”的间接级别与“ char”不同

This is an indication that something bad is going on. 这表明发生了一些不良情况。 So what happens when this line is executed? 那么执行此行会发生什么?

If input.Z_pos_Camera_Temperature has a value of 32, the 4-byte integer is truncated to 1 byte by your cast to char and assigned to the char* temp . 如果input.Z_pos_Camera_Temperature的值为32,则input.Z_pos_Camera_Temperature为char并将其分配给char* temp ,将4字节整数截断为1个字节。 temp now contains the address 0x00000020. temp现在包含地址0x00000020。

If input.Z_pos_Camera_Temperature is 450 (maybe the camera is in an oven?) the value would be truncated to from 0x000001C2 to 0xC2, sign extended when assigned, and `temp' would now contain the address 0xffffffC2; 如果input.Z_pos_Camera_Temperature是450(也许相机在烤箱里?),该值将被截断为从0x000001C2到0xC2,分配时符号会扩展,并且'temp'现在将包含地址0xffffffC2;

The second attempt is the same except that there is integer addition before the cast and assignment: 第二次尝试是相同的,除了在强制转换和赋值之前有整数加法:

char* temp = (char)450 + 32 + -5; // NOTE: the 32 here is the ASCII value for ' '

另一个选择是使用来自string.h lib的strcat函数。

You have to make the char* large enough to be able to store all the numbers. 您必须使char*足够大以能够存储所有数字。 Then you can use something like the following in order to convert the number to string: 然后,您可以使用类似以下的命令将数字转换为字符串:

int n[10] = {1, 2, 3, 4, 5888, 6, 7, 8, 9, 10};
char final[1000]; //adjust to acoomodate all the digits and spaces
int len = 0;
for (int i = 0; i < 10; i++) {
    char str[64];
    sprintf(str, "%d", n[i]);
    strcpy(final + len, str); //copy the ith number 
    len += strlen(str); //take a note of the number of digits used
    final[len] = ' '; //add a space
    len++;

}
final[len] = '\0'; //terminate the string

Try online 在线尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM