-3

I want to print some pointer value of unsigned char type.

For example:

int i = 0;
unsigned char buffer[32] = {0x00, };
(execute some operations..)
for(i = 0 ; i < 32; i++)
{
   printf("%x", buffer[i]);
}

The pointer named buffer is a result that execute operations. I had tried some ways for resolving this problems, but I can't.

The code changed "%x" to "%llu" , "%lu" , "%zu", "%zx" , "%hhx" etc..

This code is well at 32bit Linux platform(CentOS8).

For hexadecimal print, How can I do ?

1 Answers1

0

You use the %p format string to print pointers:

 printf("%p", buffer[i]);
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • This is not a correct answer about my question bacause it is only about hexadecimal. – Eunei Chae Feb 16 '21 at 06:29
  • You said "I want to print some pointer value" and %p is the format string you use for that. Your buffer contain bytes which is somewhat suspect as pointers on x64 are 64-bit long. Did you mean to ask how to print a byte in hex? That would be `printf("%.2x\n", 0x00);` if you want two digit output. – Allan Wind Feb 16 '21 at 07:50
  • I had tried what you said. However, this way is not applied. I continuously got the problem. I couldn't resolve it. what you said is well at 32bit platform. Also, what I said is too. But, 64bit platform has a problem, prints null value. For example, 0xab at 32bit platform, But 0x00 at 64bit platform. It has a same source code and operation. – Eunei Chae Feb 16 '21 at 08:26
  • You initialize the buffer to 0x0 so it will contain 32 copies of 0x00000000 , and indeed when I run your code I get 0. I think what you are telling me in your last comment is that "(execute some operations..)" doesn't update the buffer. That's entirely different problem. – Allan Wind Feb 16 '21 at 08:39
  • When I executed this code at Visual Studio platform, I got a result value that is not zero. So, I think that it is not an different problem, just print source is incorrect. – Eunei Chae Feb 16 '21 at 08:59