diff --git a/Software/src/helpers.cpp b/Software/src/helpers.cpp index 9b680a0..e1382b7 100644 --- a/Software/src/helpers.cpp +++ b/Software/src/helpers.cpp @@ -1,71 +1,89 @@ #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; - 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++; + return c - '0'; } + 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); - 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) { - 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]); - } - else - { - byte2char(&array[array_len - 2 - i], &str[i * 2]); + byte2char(array[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"); - for (int i = 0; i < array_len; i++) + for (size_t i = 0; i < array_len; i++) { Serial.printf("%02x", array[i]); } Serial.println(); -} \ No newline at end of file +} diff --git a/Software/src/helpers.h b/Software/src/helpers.h index cd100ff..3428121 100644 --- a/Software/src/helpers.h +++ b/Software/src/helpers.h @@ -2,12 +2,36 @@ #define HELPERS_H #include -void char2byte(char *str, byte *array); -void chars2bytes(char *str, byte *array, bool msb); +#include +#include +#include -void byte2char(byte *array, char *str); -void bytes2chars(byte *array, byte array_len, char *str, bool msb); +#ifdef __cplusplus +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 \ No newline at end of file + // 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