gpt4 book ai didi

C 打印 void * 缓冲区

转载 作者:行者123 更新时间:2023-11-30 18:04:02 25 4
gpt4 key购买 nike

好吧,这个

void printPacketBuffer(void *buffer, unsigned int length)
{
unsigned int i=0;
char *c;
unsigned int limit = ( (length != 0)?length:PACKET_DATA );
for (i=0; i<limit; i++) {
c = (char *)(buffer+i);
if ( *c != '\0' ) {
printf("%c", *c);
}
}
}

正在工作,但以下函数缺少最后 14 个字节,无法理解为什么?!

void printPacket (void * buffer) {
unsigned int size = getPacket_size(buffer);
printf("\n***********\nNew Packet holding %d bytes of data\n***********\n", size);
char *c;
int counter=0;
int i;
for (i=2; i<size+2; i++) {
c = (char *)(buffer+i);
if ( *c != '\0' ) {
printf("%c", *c);
counter++;
}
}
printf("\nactual printing %d, i=%d\n", counter, i);
}

这是通话

printPacketBuffer(pbuffer+2, getPacket_size(pbuffer));
printPacket(pbuffer);

最佳答案

我认为 pbuffer+2 是两个字节的下一个地址,但我错了,这就是发生的事情如果 bpuffer = 0x7fff2c03d8a0 则 bpuffer + 2 = 0x7fff2c03d8b0现在移动到我使用的下一个 2 字节位置

void *temp = (char *)pbuffer+shift;

无论如何,这个故事的主旨是使用字节转换为无符号字符

void printPacket (void * buffer) {
unsigned int size = getPacket_size(buffer);
printf("\n***********\nNew Packet holding %d bytes of data %p\n***********\n", size, buffer);
char *c;
int counter=0;
int i;
for (i=2; i<size+2; i++) {
c = (char *)buffer+i;
if ( *c != '\0' ) {
printf("%c", *c);
counter++;
}
else {
printf("NULL ");
}
}
}

关于C 打印 void * 缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7832930/

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