打印char数组的十六进制表示[]

时间:2021-01-06 20:19:43

I've got an array of 8 bytes that I'm trying to print out the hexadecimal notation for. Using printf("%x", array) I can get at the first byte and print it out but I'm getting "0xffffff9b" or something of the sort. Is there a way to get the notation without the "f's"?

我有一个8字节的数组,我正在尝试打印出十六进制表示法。使用printf(“%x”,数组)我可以得到第一个字节并打印出来,但我得到的是“0xffffff9b”或类似的东西。有没有办法在没有“f”的情况下获得符号?

I would like to print out each element looking something like:

我想打印出每个元素看起来像:

0x9a, 0x43, 0x0D, etc.

0x9a,0x43,0x0D等

2 个解决方案

#1


16  

This:

printf("%x", array);

will most likely print the address of the first element of your array in hexadecimal. I say "most likely" because the behavior of attempting to print an address as if it were an unsigned int is undefined. If you really wanted to print the address, the right way to do it would be:

很可能会以十六进制的形式打印数组第一个元素的地址。我说“最有可能”,因为尝试打印地址的行为就好像是无符号的int是未定义的。如果你真的想打印地址,正确的做法是:

printf("%p", (void*)array);

(An array expression, in most contexts, is implicitly converted to ("decays" to) a pointer to the array's first element.)

(在大多数情况下,数组表达式被隐式转换为(“衰减”)指向数组第一个元素的指针。)

If you want to print each element of your array, you'll have to do so explicitly. The "%s" format takes a pointer to the first character of a string and tells printf to iterate over the string, printing each character. There is no format that does that kind of thing in hexadecimal, so you'll have to do it yourself.

如果要打印阵列的每个元素,则必须明确地这样做。 “%s”格式采用指向字符串第一个字符的指针,并告诉printf迭代字符串,打印每个字符。没有格式以十六进制形式执行此类操作,因此您必须自己完成。

For example, given:

例如,给定:

unsigned char arr[8];

you can print element 5 like this:

你可以像这样打印元素5:

printf("0x%x", arr[5]);

or, if you want a leading zero:

或者,如果你想要一个前导零:

printf("0x%02x", arr[5]);

The "%x" format requires an unsigned int argument, and the unsigned char value you're passing is implicitly promoted to unsigned int, so this is type-correct. You can use "%x" to print the hex digits a throughf in lower case, "%X" for upper case (you used both in your example).

“%x”格式需要unsigned int参数,并且您传递的unsigned char值被隐式提升为unsigned int,因此这是类型正确的。您可以使用“%x”以小写字母打印十六进制数字af,对于大写字母打印“%X”(在您的示例中使用了两者)。

(Note that the "0x%02x" format works best if bytes are 8 bits; that's not guaranteed, but it's almost certainly the case on any system you're likely to use.)

(注意,如果字节是8位,“0x%02x”格式效果最好;这不能保证,但在您可能使用的任何系统上几乎都可以。)

I'll leave it to you to write the appropriate loop and decide how to delimit the output.

我将留给您编写适当的循环并决定如何分隔输出。

#2


3  

This is what I did, its a little bit easier with a function and I use for debugging and logging memory.

这就是我所做的,它使用一个函数稍微容易一点,我用它来调试和记录内存。

void print_hex_memory(void *mem) {
  int i;
  unsigned char *p = (unsigned char *)mem;
  for (i=0;i<128;i++) {
    printf("0x%02x ", p[i]);
    if ((i%16==0) && i)
      printf("\n");
  }
  printf("\n");
}

#1


16  

This:

printf("%x", array);

will most likely print the address of the first element of your array in hexadecimal. I say "most likely" because the behavior of attempting to print an address as if it were an unsigned int is undefined. If you really wanted to print the address, the right way to do it would be:

很可能会以十六进制的形式打印数组第一个元素的地址。我说“最有可能”,因为尝试打印地址的行为就好像是无符号的int是未定义的。如果你真的想打印地址,正确的做法是:

printf("%p", (void*)array);

(An array expression, in most contexts, is implicitly converted to ("decays" to) a pointer to the array's first element.)

(在大多数情况下,数组表达式被隐式转换为(“衰减”)指向数组第一个元素的指针。)

If you want to print each element of your array, you'll have to do so explicitly. The "%s" format takes a pointer to the first character of a string and tells printf to iterate over the string, printing each character. There is no format that does that kind of thing in hexadecimal, so you'll have to do it yourself.

如果要打印阵列的每个元素,则必须明确地这样做。 “%s”格式采用指向字符串第一个字符的指针,并告诉printf迭代字符串,打印每个字符。没有格式以十六进制形式执行此类操作,因此您必须自己完成。

For example, given:

例如,给定:

unsigned char arr[8];

you can print element 5 like this:

你可以像这样打印元素5:

printf("0x%x", arr[5]);

or, if you want a leading zero:

或者,如果你想要一个前导零:

printf("0x%02x", arr[5]);

The "%x" format requires an unsigned int argument, and the unsigned char value you're passing is implicitly promoted to unsigned int, so this is type-correct. You can use "%x" to print the hex digits a throughf in lower case, "%X" for upper case (you used both in your example).

“%x”格式需要unsigned int参数,并且您传递的unsigned char值被隐式提升为unsigned int,因此这是类型正确的。您可以使用“%x”以小写字母打印十六进制数字af,对于大写字母打印“%X”(在您的示例中使用了两者)。

(Note that the "0x%02x" format works best if bytes are 8 bits; that's not guaranteed, but it's almost certainly the case on any system you're likely to use.)

(注意,如果字节是8位,“0x%02x”格式效果最好;这不能保证,但在您可能使用的任何系统上几乎都可以。)

I'll leave it to you to write the appropriate loop and decide how to delimit the output.

我将留给您编写适当的循环并决定如何分隔输出。

#2


3  

This is what I did, its a little bit easier with a function and I use for debugging and logging memory.

这就是我所做的,它使用一个函数稍微容易一点,我用它来调试和记录内存。

void print_hex_memory(void *mem) {
  int i;
  unsigned char *p = (unsigned char *)mem;
  for (i=0;i<128;i++) {
    printf("0x%02x ", p[i]);
    if ((i%16==0) && i)
      printf("\n");
  }
  printf("\n");
}