Improve NimBLEUtils::dataToHexString efficiency.

This commit is contained in:
h2zero 2024-11-13 08:40:08 -07:00 committed by h2zero
parent beac19cc92
commit 84f4d4f897

View file

@ -498,18 +498,17 @@ const char* NimBLEUtils::gapEventToString(uint8_t eventType) {
* @return A string representation of the data. * @return A string representation of the data.
*/ */
std::string NimBLEUtils::dataToHexString(const uint8_t* source, uint8_t length) { std::string NimBLEUtils::dataToHexString(const uint8_t* source, uint8_t length) {
constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
std::string str{}; std::string str{};
str.reserve(length * 2 + 1); str.reserve(length * 2 + 1);
for (uint8_t i = 0; i < length; i++) { for (uint8_t i = 0; i < length; i++) {
char c = (source[i] >> 4) & 0x0f; str[2 * i] = hexmap[(source[i] & 0xF0) >> 4];
str.push_back(c > 9 ? c + 'A' - 10 : c + '0'); str[2 * i + 1] = hexmap[source[i] & 0x0F];
c = source[i] & 0x0f;
str.push_back(c > 9 ? c + 'A' - 10 : c + '0');
} }
return str; return str;
} // hexDataToString } // dataToHexString
/** /**
* @brief Generate a random BLE address. * @brief Generate a random BLE address.