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,69 +1,87 @@
#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++)
{
char c = *str;
if (c >= '0' && c <= '9')
{
*array *= 16;
*array += c - '0';
return c - '0';
}
else if (c >= 'A' && c <= 'F')
if (c >= 'A' && c <= 'F')
{
*array *= 16;
*array += (c - 'A') + 10;
return c - 'A' + 10;
}
else if (c >= 'a' && c <= 'f')
if (c >= 'a' && c <= 'f')
{
*array *= 16;
*array += (c - 'a') + 10;
}
str++;
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);
for (int i = 0; i < len; i += 2)
{
*value = (hex2nibble(str[0]) << 4) | hex2nibble(str[1]);
}
// 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)
{
char2byte(&str[i], &array[i / 2]);
for (size_t i = 0; i < byteCount; i++)
{
array[i] = (hex2nibble(str[2 * i]) << 4) | hex2nibble(str[2 * i + 1]);
}
}
else
{
char2byte(&str[i], &array[len - 2 - i / 2]);
}
}
}
void byte2char(byte *array, char *str)
{
sprintf(str, "%02x", *array);
}
void bytes2chars(byte *array, byte array_len, char *str, bool msb)
{
for (int i = 0; i < array_len; i++)
for (size_t i = 0; i < byteCount; i++)
{
array[byteCount - 1 - i] = (hex2nibble(str[2 * i]) << 4) | hex2nibble(str[2 * i + 1]);
}
}
}
// Convert a single byte into two hex characters.
void byte2char(uint8_t value, char *str)
{
const char hexDigits[] = "0123456789abcdef";
str[0] = hexDigits[value >> 4];
str[1] = hexDigits[value & 0x0F];
str[2] = '\0';
}
// 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)
{
if (msb)
{
byte2char(&array[i], &str[i * 2]);
for (size_t i = 0; i < array_len; i++)
{
byte2char(array[i], &str[i * 2]);
}
}
else
{
byte2char(&array[array_len - 2 - i], &str[i * 2]);
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");
for (int i = 0; i < array_len; i++)
for (size_t i = 0; i < array_len; i++)
{
Serial.printf("%02x", array[i]);
}

View File

@ -2,12 +2,36 @@
#define HELPERS_H
#include <Arduino.h>
void char2byte(char *str, byte *array);
void chars2bytes(char *str, byte *array, bool msb);
void byte2char(byte *array, char *str);
void bytes2chars(byte *array, byte array_len, char *str, bool msb);
void printbytes(byte *array, byte array_len);
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
// Convert two hex characters to a byte.
void char2byte(const char *str, uint8_t *value);
// 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