Reputation: 21
I want to convert byte array uint8_t
Hexadecimal values to char array string (**same values not ASCII) and then simple print this string array as below:
input:
uint8_t myarr[4]={0x11,0x12,0x33,0x1A}
output:
1112331A
Simple char array string not hexadecimal array string.
Upvotes: 1
Views: 2550
Reputation: 215090
The old school way of raw data to ASCII hex conversion, is to use a look-up table based on nibbles:
#include <stdint.h>
#include <stdio.h>
int main (void)
{
uint8_t myarr[4]={0x11,0x12,0x33,0x1A};
char hexstr[4][2+1] = {0};
const char HEX [16] = "0123456789ABCDEF";
for(size_t i=0; i<4; i++)
{
hexstr[i][0] = HEX[ (myarr[i] & 0xF0) >> 4 ];
hexstr[i][1] = HEX[ (myarr[i] & 0x0F) ];
puts(hexstr[i]);
}
}
Upvotes: 2
Reputation: 409442
Just loop over the elements of the array and use printf
to print each element using the "%hhx"
format specifier.
Also note that the values will not be stored as hexadecimal. Hexadecimal is just a form to present values.
Upvotes: 7