Skip to content

Commit 84f4d4f

Browse files
committed
Improve NimBLEUtils::dataToHexString efficiency.
1 parent beac19c commit 84f4d4f

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/NimBLEUtils.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,18 +498,17 @@ const char* NimBLEUtils::gapEventToString(uint8_t eventType) {
498498
* @return A string representation of the data.
499499
*/
500500
std::string NimBLEUtils::dataToHexString(const uint8_t* source, uint8_t length) {
501-
std::string str{};
501+
constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
502+
std::string str{};
502503
str.reserve(length * 2 + 1);
503504

504505
for (uint8_t i = 0; i < length; i++) {
505-
char c = (source[i] >> 4) & 0x0f;
506-
str.push_back(c > 9 ? c + 'A' - 10 : c + '0');
507-
c = source[i] & 0x0f;
508-
str.push_back(c > 9 ? c + 'A' - 10 : c + '0');
506+
str[2 * i] = hexmap[(source[i] & 0xF0) >> 4];
507+
str[2 * i + 1] = hexmap[source[i] & 0x0F];
509508
}
510509

511510
return str;
512-
} // hexDataToString
511+
} // dataToHexString
513512

514513
/**
515514
* @brief Generate a random BLE address.

0 commit comments

Comments
 (0)