转换的char *缓冲区为十六进制在C印花 [英] Convert Char * buffer to hex for printing in c

查看:143
本文介绍了转换的char *缓冲区为十六进制在C印花的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我试图打印字符缓冲区值作为十六进制值(0X%X),

Hi i tried to print the value in char buffer as hex value as ("0X%x"),

char buffer[1024]
printf("recived (0x%x) from the client\n",buffer);

但是当我编译code有警告:

but there are warning when i compile the code :

server.c:105:5: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘char *’ [-Wformat]

因此​​,如何解决这个问题????

so how to solve this problem ????

推荐答案

下面是我会怎么做:

#include <stdio.h>
int main(void) {
  unsigned char buffer[1024];
  int ii;
  // put some dummy values in the buffer:
  for(ii=0; ii<1024; ii++) buffer[ii] = ii%256;

  // printf("0x"); -- uncomment if you want to start with "0x"

  for(ii = 0; ii < 1024; ii++) {
    printf("%02x", (unsigned int)(buffer[ii]));
    if(ii%4 == 3) printf(" ");    // groups of 8: makes more readable
                                  // uncomment if you want "all one line"
    if(ii%32 == 31) printf("\n"); // ditto
  }

  printf("\n");
}

请注意,这并不preFIX与 0X 输出 - 因为它是一个大的长一些,似乎这样做很傻。但是你可以在开始添加。相反,我选择添加一些格式(空格,退货),以使输出更易于阅读:

Note this doesn't prefix the output with 0x - since it's one big long number, it seems silly to do so. But you could add it at the beginning. Instead I chose to add some formatting (spaces, returns) to make the output easier to read:

00010203 04050607 08090a0b 0c0d0e0f 10111213 14151617 18191a1b 1c1d1e1f
20212223 24252627 28292a2b 2c2d2e2f 30313233 34353637 38393a3b 3c3d3e3f
40414243 44454647 48494a4b 4c4d4e4f 50515253 54555657 58595a5b 5c5d5e5f
60616263 64656667 68696a6b 6c6d6e6f 70717273 74757677 78797a7b 7c7d7e7f
80818283 84858687 88898a8b 8c8d8e8f 90919293 94959697 98999a9b 9c9d9e9f
a0a1a2a3 a4a5a6a7 a8a9aaab acadaeaf b0b1b2b3 b4b5b6b7 b8b9babb bcbdbebf
c0c1c2c3 c4c5c6c7 c8c9cacb cccdcecf d0d1d2d3 d4d5d6d7 d8d9dadb dcdddedf
e0e1e2e3 e4e5e6e7 e8e9eaeb ecedeeef f0f1f2f3 f4f5f6f7 f8f9fafb fcfdfeff
00010203 04050607 08090a0b 0c0d0e0f 10111213 14151617 18191a1b 1c1d1e1f
20212223 24252627 28292a2b 2c2d2e2f 30313233 34353637 38393a3b 3c3d3e3f
40414243 44454647 48494a4b 4c4d4e4f 50515253 54555657 58595a5b 5c5d5e5f
60616263 64656667 68696a6b 6c6d6e6f 70717273 74757677 78797a7b 7c7d7e7f
80818283 84858687 88898a8b 8c8d8e8f 90919293 94959697 98999a9b 9c9d9e9f
a0a1a2a3 a4a5a6a7 a8a9aaab acadaeaf b0b1b2b3 b4b5b6b7 b8b9babb bcbdbebf
c0c1c2c3 c4c5c6c7 c8c9cacb cccdcecf d0d1d2d3 d4d5d6d7 d8d9dadb dcdddedf
e0e1e2e3 e4e5e6e7 e8e9eaeb ecedeeef f0f1f2f3 f4f5f6f7 f8f9fafb fcfdfeff
00010203 04050607 08090a0b 0c0d0e0f 10111213 14151617 18191a1b 1c1d1e1f
20212223 24252627 28292a2b 2c2d2e2f 30313233 34353637 38393a3b 3c3d3e3f
40414243 44454647 48494a4b 4c4d4e4f 50515253 54555657 58595a5b 5c5d5e5f
60616263 64656667 68696a6b 6c6d6e6f 70717273 74757677 78797a7b 7c7d7e7f
80818283 84858687 88898a8b 8c8d8e8f 90919293 94959697 98999a9b 9c9d9e9f
a0a1a2a3 a4a5a6a7 a8a9aaab acadaeaf b0b1b2b3 b4b5b6b7 b8b9babb bcbdbebf
c0c1c2c3 c4c5c6c7 c8c9cacb cccdcecf d0d1d2d3 d4d5d6d7 d8d9dadb dcdddedf
e0e1e2e3 e4e5e6e7 e8e9eaeb ecedeeef f0f1f2f3 f4f5f6f7 f8f9fafb fcfdfeff
00010203 04050607 08090a0b 0c0d0e0f 10111213 14151617 18191a1b 1c1d1e1f
20212223 24252627 28292a2b 2c2d2e2f 30313233 34353637 38393a3b 3c3d3e3f
40414243 44454647 48494a4b 4c4d4e4f 50515253 54555657 58595a5b 5c5d5e5f
60616263 64656667 68696a6b 6c6d6e6f 70717273 74757677 78797a7b 7c7d7e7f
80818283 84858687 88898a8b 8c8d8e8f 90919293 94959697 98999a9b 9c9d9e9f
a0a1a2a3 a4a5a6a7 a8a9aaab acadaeaf b0b1b2b3 b4b5b6b7 b8b9babb bcbdbebf
c0c1c2c3 c4c5c6c7 c8c9cacb cccdcecf d0d1d2d3 d4d5d6d7 d8d9dadb dcdddedf
e0e1e2e3 e4e5e6e7 e8e9eaeb ecedeeef f0f1f2f3 f4f5f6f7 f8f9fafb fcfdfeff

另请注意,我用的 unsigned char型缓冲 - 否则,这个数字比 127 是相互$ P更大$ PTED为负数的,例如, 128 ,而不是打印出来作为 80 ,将打印为 FFFFFF80 - 换句话说,格式说明符将被忽略

Note also that I use an unsigned char buffer - otherwise, the numbers greater than 127 are interpreted as negative numbers, and for example, 128, instead of being printed as 80, would be printed as FFFFFF80 - in other words, the format specifier would be ignored.

您可以通过留下达到同样的事情缓存字符,并更改 printf的

You could achieve the same thing by leaving buffer as char, and changing the printf to

printf("%02x", (unsigned int)(unsigned char)buffer[ii]);

但它得到相当笨拙...

but it does get rather clumsy...

这篇关于转换的char *缓冲区为十六进制在C印花的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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