C 打印 void * 缓冲区

发布于 2024-12-11 08:30:12 字数 954 浏览 0 评论 0原文

好吧,这

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);

kay so this

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);
        }
    }
}

is working but the following function short miss the last 14 bytes can't understand why ?!

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);
}

and here is the calls

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏舞 2024-12-18 08:30:12

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

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

,故事的主旨是使用字节转换为 unsigned char

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 ");
        }
    }
}

i supposed that pbuffer+2 is the next address by two bytes but I was WRONG here's what was happening
if bpuffer = 0x7fff2c03d8a0 the bpuffer + 2 = 0x7fff2c03d8b0
now to move to next 2 bytes location I used

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

anyway the morale of the story is to work with bytes cast to unsigned char

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 ");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文