Skip to content

Commit

Permalink
Improve NimBLEUtils::dataToHexString efficiency.
Browse files Browse the repository at this point in the history
  • Loading branch information
h2zero committed Nov 13, 2024
1 parent 1386043 commit e4d4ba6
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/NimBLEUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,18 +498,17 @@ const char* NimBLEUtils::gapEventToString(uint8_t eventType) {
* @return A string representation of the data.
*/
std::string NimBLEUtils::dataToHexString(const uint8_t* source, uint8_t length) {
std::string str{};
constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
std::string str{};
str.reserve(length * 2 + 1);

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

return str;
} // hexDataToString
} // dataToHexString

/**
* @brief Generate a random BLE address.
Expand Down

0 comments on commit e4d4ba6

Please sign in to comment.