将int存储到char数组并从char数组中检索

时间:2014-06-26 05:23:35

标签: c buffer printf scanf

我要做的是将两个整数存储到一个char数组中,然后从数组中取回它们。这是我的代码:

#include <stdio.h>

#define INT_WIDTH       4
#define BUFFER_SIZE     10

int main(int argc, char * argv[]) {
    char buffer[BUFFER_SIZE] = {0};
    int input1 = 0, input2 = 0;
    int output1 = 0, output2 = 0;

    printf("int size: %d\n", sizeof(int));

    printf("Please input two integers\n");
    scanf("%d", &input1);
    scanf("%d", &input2);
    printf("The input integers are : %d %d\n", input1, input2);

    snprintf(buffer, BUFFER_SIZE, "%d", input1);
    snprintf(buffer + INT_WIDTH, BUFFER_SIZE - INT_WIDTH, "%d", input2);

    printf("Buffer:\n");
    for (int i = 0; i < BUFFER_SIZE; ++i) {
        printf("0x%02x ", buffer[i]);
    }
    printf("\n");

    sscanf(buffer, "%d", &output1);
    sscanf(buffer + INT_WIDTH, "%d", &output2);

    printf("The output integers retrieved from buffer: %d %d\n", output1, output2);

    return 0;
}

这是输出:

int size: 4
Please input two integers
100
200
The input integers are : 100 200
Buffer:
0x31 0x30 0x30 0x00 0x32 0x30 0x30 0x00 0x00 0x00
The output integers retrieved from buffer: 100 200

看起来很好。但是当我输入1000和2000而不是100和200时,事情就出错了:

int size: 4
Please input two integers
1000
2000
The input integers are : 1000 2000
Buffer:
0x31 0x30 0x30 0x30 0x32 0x30 0x30 0x30 0x00 0x00
The output integers retrieved from buffer: 10002000 2000

我的代码出错了吗?

同样,我尝试了以下代码:

#include <stdio.h>

#define BUFFER_SIZE     10

int main(int argc, char * argv[]) {
    char buffer[BUFFER_SIZE] = {0};
    int input1 = 0, input2 = 0;
    int output1 = 0, output2 = 0;

    printf("Please input two integers\n");
    scanf("%d", &input1);
    scanf("%d", &input2);
    printf("The input integers are : %d %d\n", input1, input2);

    snprintf(buffer, BUFFER_SIZE, "%4d%4d", input1, input2);

    printf("Buffer:\n");
    for (int i = 0; i < BUFFER_SIZE; ++i) {
        printf("0x%02x ", buffer[i]);
    }
    printf("\n");

    sscanf(buffer, "%4d%4d", &output1, &output2);

    printf("The output integers retrieved from buffer: %d %d\n", output1, output2);

    return 0;
}

这是输出:

Please input two integers
1000
2000
The input integers are : 1000 2000
Buffer:
0x31 0x30 0x30 0x30 0x32 0x30 0x30 0x30 0x00 0x00
The output integers retrieved from buffer: 1000 2000

Please input two integers
10000
20000
The input integers are : 10000 20000
Buffer:
0x31 0x30 0x30 0x30 0x30 0x32 0x30 0x30 0x30 0x00
The output integers retrieved from buffer: 1000 200

如您所见,1000和2000可以,10000和20000是错误的。 我哪里错了?

3 个答案:

答案 0 :(得分:2)

您不存储整数,而是存储数字字符的字符串。在行中:

snprintf(buffer, BUFFER_SIZE, "%d", input1);
snprintf(buffer + INT_WIDTH, BUFFER_SIZE - INT_WIDTH, "%d", input2);

第二个snprintf()将覆盖第一个字符串的nul终结符,并将数字紧邻第一个字符串放置。 sscanf()需要whitespece来分隔字段。在第二个版本中,您只是用完了缓冲区空间。你为什么感到惊讶 - 你试图写12个字符,但BUFFER_SIZE是10?

我想知道你为什么不能将整数存储在一个int数组中?如果你必须存储数字字符串,为什么不使用二维数组而不是搞乱所有的缓冲区索引操作。

char buffer[2][INT_WIDTH] = {{0},{0}} ;
...
snprintf( buffer[0], INT_WIDTH, "%d", input1 ) ;
snprintf( buffer[1], INT_WIDTH, "%d", input2 ) ;
...
sscanf( buffer[0], "%d", &output1 ) ;
sscanf( buffer[1], "%d", &output1 ) ;

要将任意长度的32位有符号整数存储为字符串,您需要INT_WIDTH为12;最多10位数,可能的负号和nul终结符。

答案 1 :(得分:1)

正如Clifford指出的那样,我存储了数字字符的字符串,而不是存储整数的二进制内容。这是我真正想要的代码:

#include <stdio.h>
#include <string.h>

#define BUFFER_SIZE     10

int main(int argc, char * argv[]) {
    char buffer[BUFFER_SIZE] = {0};
    int input = 0, output = 0;

    printf("size of int: %d\n", sizeof(int));

    printf("Please input an integer\n");
    scanf("%d", &input);
    printf("The input integer is : %d\n", input);

    memcpy(buffer, &input, sizeof(int));

    printf("Buffer:\n");
    for (int i = 0; i < BUFFER_SIZE; ++i) {
            printf("0x%02x ", buffer[i]);
    }
    printf("\n");

    memcpy(&output, buffer, sizeof(int));

    printf("The output integer retrieved from buffer: %d\n", output);

    return 0;
}

输出:

size of int: 4
Please input an integer
1024
The input integer is : 1024
Buffer:
0x0 0x4 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
The output integer retrieved from buffer: 1024

size of int: 4
Please input an integer
2147483647      (2 ^ 31 - 1)
The input integer is : 2147483647
Buffer:
0xffffffff 0xffffffff 0xffffffff 0x7f 0x0 0x0 0x0 0x0 0x0 0x0
The output integer retrieved from buffer: 2147483647

size of int: 4
Please input an integer
2147483648      (2 ^ 31)
The input integer is : -2147483648
Buffer:
0x0 0x0 0x0 0xffffff80 0x0 0x0 0x0 0x0 0x0 0x0
The output integer retrieved from buffer: -2147483648

P.S。

我想在char数组中存储int的原因是需要将一个整数标志从指定位置设置到二进制缓冲区中,并在缓冲区中占用4个字节。

答案 2 :(得分:0)

请描述一下你想做什么? 为什么要在CHAR数组中存储INT?

问题是,根据您的硬件,INT的大小可能比CHAR大。 您可以使用sizeof(int)和sizeof(char)来检查它。

通常CHAR将具有8Bit(1Byte),INT将具有32Bit(4Byte)。 如此大于255的数字将不适合CHAR变量!