0

//How to conversion the number 12345 change to the unsigned char*(why is '90') and how to modify code to show the hex value (eg.0x80...)?

#include <stdio.h>
#include <stdlib.h>

typedef unsigned char *byte_pointer;

void show_bytes(byte_pointer, size_t);
void show_bits(int);
void show_int(int);
void show_float(float);
void show_pointer(void *) ;
int  mask_LSbits(int);



void show_bytes(byte_pointer start, size_t len)
{
    size_t i;
    printf("address=%p",start);
    printf("\n");
    for (i = 0; I <len; i++)
    printf(" %x.2x", start[i]);  //modify here, how to change char to hex value
    printf("\n");
}
void show_int(int x) {
    printf("\nival = %d\n", x);
    show_bytes((byte_pointer) &x, sizeof(int));
} 
int main()
{
    int ival = 12345;
    float fval = (float) ival;
    int *pval = &ival;

    show_int(ival);

    return 0;

}
niry
  • 3,238
  • 22
  • 34
Richard
  • 31
  • 3
  • 2
    Surely `%x` *does* show the character's hex value? the format `%x` expects an `unsigned int` but in a variadic function like `printf` a `char` argument is promoted to `int` anyway. For some reason, the coder instructed `printf` to output `.2x` after every hex value. – Weather Vane Jan 18 '20 at 18:40
  • Does this answer your question? [C print hex bytes](https://stackoverflow.com/questions/46145173/c-print-hex-bytes) – niry Jan 18 '20 at 18:46
  • See [Is it a good idea to typedef pointers?](https://stackoverflow.com/questions/750178/) — TL;DR it's usually not a good idea. – Jonathan Leffler Jan 18 '20 at 19:09

1 Answers1

3

To display each of the four component bytes of the int value passed (after casting) to your show_bytes function (assuming you want these in the format 0x01), change your printf format string as follows:

printf(" 0x%02x", (unsigned int)start[i]);

The 0x is printed as is; the %02x formats the actual value of start[i] in hexadecimal, always using a field width of 2 and adding leading zeros, if required.

Note that the %x format expects an unsigned int as the corresponding argument, which is why I've added the cast.

The output I get when I run your code (with my suggested modification) is:

ival = 12345
address = 000000CE690FFA30
0x39 0x30 0x00 0x00

Which corresponds to an integer with the following (full) hex value: 0x00003039 - which is 12345, in decimal.

Note, also, that if you add the following line:

printf("address=%p", pval);

in your main function, you will see that the value of the pval address is not the same as the value printed in your show_bytes function! This is because your are passing a copy of ival to show_int, which is placed (most probably) on the stack, and thus is, effectively, a different variable from the original (which pval points to).

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • I also want to know if the decimal is 12345 and it binary is 0011 0000 0011 1001 which is 3039, is the number's order matter? If I just want to print the ans is 0x3039, what should I do? Also the Len is four which take the number (3)(0)(3)(9) what other two 0x00 use to do? Thanks – Richard Jan 18 '20 at 19:05
  • The 'other two' `0x00` values are the upper two bytes of the integer. Each of the four bytes is displayed as a **two-digit** hex number. If you want to 'merge' the data into `0x3039` (actually, `0x00003039`) you will need to (a) reverse the order of printing and (b) just print the leading `0x` once (before the loop) and just use a plain `%02x` format inside the loop. – Adrian Mole Jan 18 '20 at 19:14
  • 1
    Nominally, `%p` is for printing a `void *`. Granted the `char *` type is closely related (C11 [§6,2,5 Types ¶28](https://port70.net/~nsz/c/c11/n1570.html#6.2.5p28)), but … Also, you could almost use `%#.2x` except that (for some reason I do not understand), the `0x` prefix is not printed when the value is `0`. – Jonathan Leffler Jan 18 '20 at 19:16
  • Also, maybe good to have a read about [endianness](https://en.wikipedia.org/wiki/Endianness) – Adrian Mole Jan 18 '20 at 19:16
  • @Jonathan Maybe we need somebody to post a **full canonical** Q/A on `printf` and `scanf` formats and usage? There's a good, steady flow of questions here that could be pointed thence! – Adrian Mole Jan 18 '20 at 19:19
  • 2
    There's a limit to the size of essay allowed as a single answer in SO :( Besides, writing new answers is good for the soul. – Jonathan Leffler Jan 18 '20 at 19:20