optimize helper code

This commit is contained in:
André Fiedler 2025-02-15 18:26:51 +01:00
parent f5f663771f
commit 4af6ac8975
2 changed files with 87 additions and 45 deletions

View File

@ -1,71 +1,89 @@
#include "helpers.h" #include "helpers.h"
void char2byte(char *str, byte *array) // Helper: convert a single hex char into a nibble (4 bits)
static inline uint8_t hex2nibble(char c)
{ {
for (int i = 0; i < 2; i++) if (c >= '0' && c <= '9')
{ {
char c = *str; return c - '0';
if (c >= '0' && c <= '9')
{
*array *= 16;
*array += c - '0';
}
else if (c >= 'A' && c <= 'F')
{
*array *= 16;
*array += (c - 'A') + 10;
}
else if (c >= 'a' && c <= 'f')
{
*array *= 16;
*array += (c - 'a') + 10;
}
str++;
} }
if (c >= 'A' && c <= 'F')
{
return c - 'A' + 10;
}
if (c >= 'a' && c <= 'f')
{
return c - 'a' + 10;
}
return 0; // or handle error
} }
void chars2bytes(char *str, byte *array, bool msb) // Convert two hex characters to a byte.
void char2byte(const char *str, uint8_t *value)
{ {
int len = strlen(str); *value = (hex2nibble(str[0]) << 4) | hex2nibble(str[1]);
for (int i = 0; i < len; i += 2) }
// Convert a hex string (2 chars per byte) into a byte array.
// If msb is true, the first two chars go into array[0]; if false, the byte order is reversed.
void chars2bytes(const char *str, uint8_t *array, bool msb)
{
size_t len = strlen(str);
size_t byteCount = len / 2;
if (msb)
{ {
if (msb) for (size_t i = 0; i < byteCount; i++)
{ {
char2byte(&str[i], &array[i / 2]); array[i] = (hex2nibble(str[2 * i]) << 4) | hex2nibble(str[2 * i + 1]);
} }
else }
else
{
for (size_t i = 0; i < byteCount; i++)
{ {
char2byte(&str[i], &array[len - 2 - i / 2]); array[byteCount - 1 - i] = (hex2nibble(str[2 * i]) << 4) | hex2nibble(str[2 * i + 1]);
} }
} }
} }
void byte2char(byte *array, char *str) // Convert a single byte into two hex characters.
void byte2char(uint8_t value, char *str)
{ {
sprintf(str, "%02x", *array); const char hexDigits[] = "0123456789abcdef";
str[0] = hexDigits[value >> 4];
str[1] = hexDigits[value & 0x0F];
str[2] = '\0';
} }
void bytes2chars(byte *array, byte array_len, char *str, bool msb) // Convert an array of bytes to a hex string.
// If msb is true, the order is maintained; if false, the order is reversed.
void bytes2chars(const uint8_t *array, size_t array_len, char *str, bool msb)
{ {
for (int i = 0; i < array_len; i++) if (msb)
{ {
if (msb) for (size_t i = 0; i < array_len; i++)
{ {
byte2char(&array[i], &str[i * 2]); byte2char(array[i], &str[i * 2]);
}
else
{
byte2char(&array[array_len - 2 - i], &str[i * 2]);
} }
} }
else
{
for (size_t i = 0; i < array_len; i++)
{
byte2char(array[array_len - 1 - i], &str[i * 2]);
}
}
str[array_len * 2] = '\0';
} }
void printbytes(byte *array, byte array_len) // Print a byte array in hexadecimal format.
void printbytes(const uint8_t *array, size_t array_len)
{ {
Serial.print("0x"); Serial.print("0x");
for (int i = 0; i < array_len; i++) for (size_t i = 0; i < array_len; i++)
{ {
Serial.printf("%02x", array[i]); Serial.printf("%02x", array[i]);
} }
Serial.println(); Serial.println();
} }

View File

@ -2,12 +2,36 @@
#define HELPERS_H #define HELPERS_H
#include <Arduino.h> #include <Arduino.h>
void char2byte(char *str, byte *array); #include <stdint.h>
void chars2bytes(char *str, byte *array, bool msb); #include <stddef.h>
#include <stdbool.h>
void byte2char(byte *array, char *str); #ifdef __cplusplus
void bytes2chars(byte *array, byte array_len, char *str, bool msb); extern "C"
{
#endif
void printbytes(byte *array, byte array_len); // Convert two hex characters to a byte.
void char2byte(const char *str, uint8_t *value);
#endif // Convert a hex string (2 characters per byte) into a byte array.
// If msb is true, the first two characters go into array[0];
// if false, the resulting bytes are stored in reverse order.
void chars2bytes(const char *str, uint8_t *array, bool msb);
// Convert a single byte into two hex characters.
void byte2char(uint8_t value, char *str);
// Convert an array of bytes to a hex string.
// If msb is true, the order is maintained;
// if false, the byte order is reversed.
void bytes2chars(const uint8_t *array, size_t array_len, char *str, bool msb);
// Print a byte array in hexadecimal format.
void printbytes(const uint8_t *array, size_t array_len);
#ifdef __cplusplus
}
#endif
#endif // HELPERS_H