Link to home
Create AccountLog in
Avatar of srini_kandimalla
srini_kandimalla

asked on

How to convert byte array to hex string array in C

How to convert byte array to hex string array in C.

Provide me sample function.
Avatar of jkr
jkr
Flag of Germany image

To convert a single byte of the array, you can use 'sprintf()' (http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/) with the '%x' format specifier:
char hex[3];
unsigned char my_byte = 0x42;

sprintf(hex,"%2.2x", my_byte);

Open in new window


Now all you need to do is to build that into a loop and apply that to each array element consecutively.
Avatar of srini_kandimalla
srini_kandimalla

ASKER

When I ran this sample code and try to access hex[3], it is not printing anything and hex[0] is showing as null.
Any better ways?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer