Skip to content

Instantly share code, notes, and snippets.

@J-Rios
Created February 26, 2022 19:26
Show Gist options
  • Save J-Rios/31577e31a131162d0a20a2fc68627be0 to your computer and use it in GitHub Desktop.
Save J-Rios/31577e31a131162d0a20a2fc68627be0 to your computer and use it in GitHub Desktop.
C Byte Array To Hexadecimal String Conversion
/* Libraries */
// Standard libc Library
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
/*****************************************************************************/
/* Defines */
// Hexadecimal byte array size
#define BYTE_ARRAY_SIZE 8
// Hexadecimal string max size
#define HEX_STR_SIZE 17
/*****************************************************************************/
/* Function Prototypes */
/**
* @brief Convert bytes array to hexadecimal representation string.
* @param byte_array Bytes array to convert.
* @param byte_array_size Number of bytes to convert from the array.
* @param hex_str Converted hexadecimal string.
* @param hex_str_size Maximum size of the hexadecimal string.
* @return Conversion result success/fail (true/false).
*/
bool byte_array_2_hex_str(
const uint8_t* byte_array, const uint16_t byte_array_size,
char* hex_str, const uint16_t hex_str_size);
/**
* @brief Convert an unsigned integer of 8 bits to hexadecimal string.
* @param n Byte number to convert.
* @param hex_str Converted hexadecimal string.
* @param hex_str_size Hexadecimal string size.
* @return Conversion result success/fail (true/false).
*/
bool hex_2_str(uint8_t n, char* hex_str, const uint8_t hex_str_size);
/**
* @brief Convert a number digit to hexadecimal character.
* @param n NUmber digit to convert.
* @param c Converted hexadecimal character.
* @return Conversion result success/fail (true/false).
*/
bool hex_2_char(const uint8_t n, char* c);
/*****************************************************************************/
/* Main Function */
int main()
{
uint8_t byte_array[BYTE_ARRAY_SIZE] =
{ 0xfe, 0xcd, 0xab, 0x98, 0x76, 0x54, 0x32, 0x10 };
char hex_str[HEX_STR_SIZE] = "\0";
bool fail = false;
fail = !byte_array_2_hex_str(byte_array, BYTE_ARRAY_SIZE,
hex_str, HEX_STR_SIZE);
if (fail)
{
printf("\nConversion Fail\n");
return 1;
}
printf("\nConversion Success\n");
printf("byte_array: ");
for (uint8_t i = 0; i < BYTE_ARRAY_SIZE; i++)
printf("%02X", byte_array[i]);
printf("\nhex_str: %s\n", hex_str);
printf("\n");
return 0;
}
/*****************************************************************************/
/* Auxiliary Functions */
/**
* @details
* To make the conversion, it go through each byte on the array and using
* hex_2_str() function it gets the corresponding hexadecimal representation
* characters making an string.
* The provided bytes array must not be empty and the output string must has
* enough size to store the full conversion of bytes
* (hex_str_size >= (byte_array_size * 2)).
*/
bool byte_array_2_hex_str(
const uint8_t* byte_array, const uint16_t byte_array_size,
char* hex_str, const uint16_t hex_str_size)
{
// Check for valid str, has 2 or more chars and is not an odd string
if (byte_array == NULL)
return false;
if (byte_array_size == 0)
return false;
// Check if out hex string has enough size to store the conversion
// Note that: (byte_array_size << 1) == (byte_array_size * 2)
if (hex_str_size < (byte_array_size << 1))
return false;
// Convert each byte of the array
for (uint16_t i = 0; i < byte_array_size; i++)
{
if (!hex_2_str(byte_array[i], hex_str, 3))
return false;
hex_str = hex_str + 2;
}
return true;
}
/**
* @details
* To convert an uint8_t data type value into an hexadecimal character string
* the byte needs to be splited into high and low nibbles and convert them to
* char using hex_2_char() function.
*/
bool hex_2_str(uint8_t n, char* hex_str, const uint8_t hex_str_size)
{
uint8_t n_left_nibble = (n >> 4) & 0x0F;
uint8_t n_right_nibble = n & 0x0F;
char c = '\0';
if (hex_str == NULL)
return false;
if (hex_str_size < 3)
return false;
hex_str[0] = '\0';
if (!hex_2_char(n_left_nibble, &c))
return false;
hex_str[0] = c;
if (!hex_2_char(n_right_nibble, &c))
return false;
hex_str[1] = c;
hex_str[2] = '\0';
return true;
}
/**
* @details
* To convert an number digit into a hexadecimal character, it checks for
* expected valid values (0-15) and add the corresponding ASCII code offset
* to point into '0'-'9' and 'A' to 'F' character.
* Check the ASCII Encode Table:
* https://upload.wikimedia.org/wikipedia/commons/1/1b/ASCII-Table-wide.svg
*/
bool hex_2_char(const uint8_t n, char* c)
{
if ((n >= 0) && (n <= 9))
*c = (char)(n) + '0';
else if ((n >= 10) && (n <= 15))
*c = (char)(n) + 'A' - (char)(10);
else
return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment