C 打印 void * 缓冲区
好吧,这
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 pbuffer+2 是两个字节的下一个地址,但我错了,这就是发生的事情
如果 bpuffer = 0x7fff2c03d8a0 则 bpuffer + 2 = 0x7fff2c03d8b0
现在移动到我使用的下一个 2 字节位置
,故事的主旨是使用字节转换为 unsigned char
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
anyway the morale of the story is to work with bytes cast to unsigned char