gpt4 book ai didi

c - 尝试将短整型写入缓冲区开始会产生奇怪的输出

转载 作者:行者123 更新时间:2023-11-30 15:14:09 28 4
gpt4 key购买 nike

我已经被这个问题困扰了一段时间,但似乎找不到解决方案

为了简要描述我想做的事情......我正在尝试生成随机数据缓冲区(作为一行上传到测试表中)。然而,我正在使用一个系统,该系统要求我首先生成数据并填充缓冲区,然后在缓冲区的开头附加一个 2 字节长度说明符,以便它知道需要读入多少字节系统,最后是结束符。

我已经实现了生成器和大部分逻辑(已测试),但由于某种原因我无法将两个字节长度说明符正确地放入缓冲区的开头。

    int generate() {
char buffer[BUFSIZ];
memset(buffer, '\0', BUFFLEN);
unsigned short rec_len = insert_data(buffer);

//... continue

return 0;
}

unsigned short insert_data(char* buff_ptr) {
// Save a pointer to the start of the buffer
char *sbuff_ptr = buff_ptr;

// Save space to fit two bytes length specifier to be filled
// once all my data has been generated and put into the buffer
buff_ptr += 2;

// Set in the two byte record length specifier
unsigned short rec_len = buff_ptr - sbuff_ptr;
memcpy(sbuff_ptr, &rec_len, 2);

// Generate data using self-implemented data generator functions
// For example, to generate the first data value
int id = rand_int(0); // id = 0
memcpy(buff_ptr, &id, 4);
buff_ptr += 4;

// ... This continues on until all required dummy data has been
// generated

// Set in the end of record marker to indicate end of data row
memset(buff_ptr, '\n', 1);
buff_ptr += 1;

// Set in the two byte record length specifier
unsigned short rec_len = buff_ptr - sbuff_ptr;
memcpy(sbuff_ptr, &rec_len, 2);

if (DEBUG) printf("\n\tBUFF_PTR (END): %p\n", buff_ptr);
if (DEBUG) printf("\tSBUFF_PTR (START): %p\n", sbuff_ptr);
if (DEBUG) printf("\tRECORD_LENGTH: %hu\n", rec_len);

return 0;
}

Output
-----------
BUFF_PTR (END): 0xffff9169
SBUFF_PTR (START): 0xffff90ce
RECORD_LENGTH: 155

// Record Length: Confirmed to be correct (after manually counting up all the bytes

但是,当我尝试打印缓冲区的内容作为验证时检查长度说明符是否设置正确

    unsigned int i;
for(i = 0; i < rec_len; i++) {
printf("\tbyte %d BUFFER's CONTENTS: %d\n", i, buffer[i]);
}

byte 0 BUFFER's CONTENTS: -101 // This should be 155
byte 1 BUFFER's CONTENTS: 0 // End of two byte length specifier
byte 2 BUFFER's CONTENTS: 0 // int id = 0
byte 3 BUFFER's CONTENTS: 0
byte 4 BUFFER's CONTENTS: 0
byte 5 BUFFER's CONTENTS: 0
byte 6 BUFFER's CONTENTS: 110 // Start of some string value
byte 7 BUFFER's CONTENTS: 111
byte 8 BUFFER's CONTENTS: 110
byte 9 BUFFER's CONTENTS: 118
...
byte 154 BUFFER's CONTENTS: 10 // Correct termination

在尝试调试时,我编写了一个小脚本来模仿我想要做的事情,奇怪的是,它有效......比较两者,我找不到区别

#include <unistd.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

unsigned short buf_init(char* buffer) { // -- Following are the digit
// representations of each string character
char* hello = "hello\0"; // 104, 101, 108, 108, 111, 0
char* goodbye = "goodbye\0"; // 103, 111, 111, 100, 98, 121, 101, 0
char* who = "they\0"; // 116, 104, 101, 121, 0

int a = 19;
float b = 20.5;
short c = 12;

char *bp = buffer;
char *bs = buffer;

// length specifier
bp += 2;

memcpy(bp, hello, strlen(hello));
bp += strlen(hello);

memcpy(bp, &a, 4);
bp += 4;

memcpy(bp, goodbye, strlen(goodbye));
bp += strlen(goodbye);

memcpy(bp, &b, 4);
bp += 4;

memcpy(bp, who, strlen(who));
bp += strlen(who);

memcpy(bp, &c, 2);
bp += 2;

// Terminate
memset(bp, '\n', 1);
bp += 1;

// Record Length
unsigned short len = bp - bs;
memcpy(bs, &len, 2);

printf("rec_len: %hu\n", len);

return len;
}


int main(void) {
char buffer[BUFSIZ];
memset(buffer, '\0', BUFSIZ);

unsigned short len = buf_init(buffer);

int i = 0;
for(i = 0; i < len; i++) {
printf("\tbyte %d BUFFER's CONTENTS: %d\n", i, buffer[i]);
}

return 0;
}


/* --OUTPUT
rec_len: 29
byte 0 BUFFER's CONTENTS: 29 // Correct Length Specifier
byte 1 BUFFER's CONTENTS: 0
byte 2 BUFFER's CONTENTS: 104 // "Hello"
byte 3 BUFFER's CONTENTS: 101
byte 4 BUFFER's CONTENTS: 108
byte 5 BUFFER's CONTENTS: 108
byte 6 BUFFER's CONTENTS: 111
byte 7 BUFFER's CONTENTS: 19
byte 8 BUFFER's CONTENTS: 0
byte 9 BUFFER's CONTENTS: 0
byte 10 BUFFER's CONTENTS: 0
byte 11 BUFFER's CONTENTS: 103
byte 12 BUFFER's CONTENTS: 111
byte 13 BUFFER's CONTENTS: 111
byte 14 BUFFER's CONTENTS: 100
byte 15 BUFFER's CONTENTS: 98
byte 16 BUFFER's CONTENTS: 121
byte 17 BUFFER's CONTENTS: 101
byte 18 BUFFER's CONTENTS: 0
byte 19 BUFFER's CONTENTS: 0
byte 20 BUFFER's CONTENTS: -92
byte 21 BUFFER's CONTENTS: 65
byte 22 BUFFER's CONTENTS: 116
byte 23 BUFFER's CONTENTS: 104
byte 24 BUFFER's CONTENTS: 101
byte 25 BUFFER's CONTENTS: 121
byte 26 BUFFER's CONTENTS: 12
byte 27 BUFFER's CONTENTS: 0
byte 28 BUFFER's CONTENTS: 10 // Terminates correctly with an new line
*/

有人愿意提供建议吗?

最佳答案

数据存储得恰到好处。问题在于您输出数据的方式。您尝试输出使用单个 char 的缓冲区。

由于您使用的是小端系统,因此您的短字节顺序将被颠倒。这意味着最低有效字节将是第一个。从您的代码来看,short 类型在您的系统上是两个字节。值 155 将完全存储在最低有效字节中,由于它首先位于内存中,因此该字节将位于缓冲区的第一个字节中。

接下来输出缓冲区。该缓冲区使用单个charchar 可以是有符号的,也可以是无符号的,具体取决于实现。在您的实现中,它似乎已签名。 155 的值在内存中表示如下(假设是 2 的补码 8 位系统):

10011011

如您所见,第一位已设置。在 2 的补码系统中,这意味着有符号类型的值为负。由于 char 在您的系统上似乎是有符号的,因此在无符号表示中为 155 的相同序列意味着 -101 作为有符号值。然后打印该值,这将导致您看到的值 -101。

您在检查中使用的值低于 127,这意味着不会设置符号位,并且该值将按预期打印。如果您的测试消息长度超过 127 个字节,由于 2 的补码系统的工作方式,您将再次看到负值。

您可以通过将类型 char 转换为 unsigned char 来解决此问题,以确保将它们打印为无符号。另外,不要忘记编辑 printf 修饰符以表示无符号类型,而不是有符号类型。这样您就会看到预期的输出。

关于c - 尝试将短整型写入缓冲区开始会产生奇怪的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34162700/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com