Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing the hexadecimal representation of a char array[]

Tags:

c

hex

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"?

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

0x9a, 0x43, 0x0D, etc.

like image 420
MCP Avatar asked Apr 29 '12 21:04

MCP


People also ask

How do you print an array in hexadecimal?

This C function requires two parameters: the char array (pointer) and the length to print. Then, it iterates over each byte, and convert the ASCII value to Hexadecimal value – and print it out in the form of “0x??” to the console.

How to print hex value array in C?

If you have a string that represents a number in hex, you can use strtol like you have: char s[] = "ff2d"; int n = strtol(s, NULL, 16); printf("Number: %d\n", n); When you want to print the characters of a string in hex, use %x format specifier for each character of the string. Save this answer.

How do you print hexadecimal?

Printing the number in Hexadecimal format To print integer number in Hexadecimal format, "%x" or "%X" is used as format specifier in printf() statement. "%x" prints the value in Hexadecimal format with alphabets in lowercase (a-f). "%X" prints the value in Hexadecimal format with alphabets in uppercase (A-F).

How do I print a buffer in hex?

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);


3 Answers

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:

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.

For example, given:

unsigned char arr[8];

you can print element 5 like this:

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).

(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.)

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

like image 87
Keith Thompson Avatar answered Oct 15 '22 23:10

Keith Thompson


Print a string in hex:

void print_hex(const char *string)
{
        unsigned char *p = (unsigned char *) string;

        for (int i=0; i < strlen(string); ++i) {
                if (! (i % 16) && i)
                        printf("\n");

                printf("0x%02x ", p[i]);
        }
        printf("\n\n");
}

char *umlauts = "1 ä ö ü  Ä Ö Ü  é É  ß € 2";

print_hex(umlauts);

0x31 0x20 0xc3 0xa4 0x20 0xc3 0xb6 0x20 0xc3 0xbc 0x20 0x20 0xc3 0x84
0x20 0xc3 0x96 0x20 0xc3 0x9c 0x20 0x20 0xc3 0xa9 0x20 0xc3 0x89 0x20
0x20 0xc3 0x9f 0x20 0xe2 0x82 0xac 0x20 0x32
like image 30
sbin_bash Avatar answered Oct 15 '22 22:10

sbin_bash


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");
}
like image 8
Andy Avatar answered Oct 15 '22 22:10

Andy