Convert uint8_t array to char* HEX representation in C

C

Long story short, this function just creates a char array which contains the HEX representation of a uint8_t (bytes) array. Useful if you don’t want to use sprintf etc..

static char hex[] = "0123456789ABCDEF"; 
void convert_to_hex_str(char* str, uint8_t* val, size_t val_count)
{
	for (size_t i = 0; i < val_count; i++)
	{
		str[(i * 2) + 0] = hex[((val[i] & 0xF0) >> 4)];
		str[(i * 2) + 1] = hex[((val[i] & 0x0F) >> 0)];
	}
}

Disclaimer: The present content may not be used for training artificial intelligence or machine learning algorithms. All other uses, including search, entertainment, and commercial use, are permitted.

Categories

Tags