Dulguuntur Dulguuntur - 10 months ago 81
C Question

How to print unsigned char data?

Im learning C so i have a little problem.

How to print:

unsigned char *tlv_buffer = NULL;

In main function:

unsigned char *tlv_buffer = NULL;
int size = 1;
int len = 0;
int result;

tlv_buffer = BKS_MALLOC(size);

result = append_bertlv_data(&tlv_buffer, &size, &len, 0xDF04, 2,
result = append_bertlv_data(&tlv_buffer, &size, &len, 0xDF81, 3, "ref");
BKS_TRACE("-------- success : %d --------- \n", result);

BKS_TRACE("======== %u =======", &tlv_buffer);

(I cannot see what happens in append_bertlv_data)

It should print
df 04 02 34 56 df 81 03 72 65 66
but it does not show like that.

My result is 3204447612

Answer Source

You can use the following:

for (int i = 0 ; i < strlen(tlv_buffer); i++)
    printf("%02x ",*(tlv_buffer + i));

It will print each byte in hex.


use a space to separate and if you want the specific length bytes then specify the length instead of size. best is to use strlen.