打印十六进制的数组字符缓冲区 [英] Printing char buffer in hex array

查看:94
本文介绍了打印十六进制的数组字符缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读512个字符到缓冲区,并想以十六进制显示它们。我尝试以下方法,但它只是所有的时间输出值相同,尽管不同的值应该在网络上被接收到。

I am reading 512 chars into a buffer and would like to display them in hex. I tried the following approach, but it just outputs the same value all the time, despite different values should be received over the network.

char buffer[512];
bzero(buffer, 512);
n = read(connection_fd,buffer,511);
if (n < 0) printf("ERROR reading from socket");
printf("Here is the message(%d): %x\n\n", sizeof(buffer), buffer);

难道我在这里输出缓冲区数组的地址,而不是它的内容?有没有用C一个简单的方法完成这个任务还是我需要写我自己的子程序?

Is it possible that here I am outputting the address of the buffer array, rather than its content? Is there an easy way in C for this task or do I need to write my own subroutine?

推荐答案

这将读取相同的512字节的缓冲区,但相互转换的字符的十六进制输出:

This will read the same 512 byte buffer, but convert each character to hex on output:

char buffer[512];
bzero(buffer, 512);
n = read(connection_fd,buffer,511);
if (n < 0) printf("ERROR reading from socket");

printf("Here is the message:n\n");
for (int i = 0; i < n; i++)
{
    printf("%02X", buffer[i]);
}

这篇关于打印十六进制的数组字符缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆