1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 05:12:57 +01:00

sys/fmt: add function for converting hex byte

This commit is contained in:
Toon Stegen 2018-08-01 18:09:41 +02:00
parent 861fbe9a11
commit 9f90f31291
2 changed files with 17 additions and 1 deletions

View File

@ -132,6 +132,11 @@ static uint8_t _hex_nib(uint8_t nib)
return _byte_mod25((nib & 0x1f) + 9);
}
uint8_t fmt_hex_byte(const char *hex)
{
return (_hex_nib(hex[0]) << 4) | _hex_nib(hex[1]);
}
size_t fmt_hex_bytes(uint8_t *out, const char *hex)
{
size_t len = fmt_strlen(hex);
@ -143,7 +148,7 @@ size_t fmt_hex_bytes(uint8_t *out, const char *hex)
size_t final_len = len >> 1;
for (size_t i = 0, j = 0; j < final_len; i += 2, j++) {
out[j] = (_hex_nib(hex[i]) << 4) | _hex_nib(hex[i+1]);
out[j] = fmt_hex_byte(hex + i);
}
return final_len;

View File

@ -91,6 +91,17 @@ size_t fmt_bytes_hex(char *out, const uint8_t *ptr, size_t n);
*/
size_t fmt_bytes_hex_reverse(char *out, const uint8_t *ptr, size_t n);
/**
* @brief Converts a sequence of two hex characters to a byte
*
* The hex characters sequence must contain valid hexadecimal characters
* otherwise the result is undefined.
*
* @param[in] hex Pointer to input buffer
* @returns byte based on hex string
*/
uint8_t fmt_hex_byte(const char *hex);
/**
* @brief Converts a sequence of hex bytes to an array of bytes
*