Skip to content

Commit 3406ff2

Browse files
committed
CpuUid: to save memory, overloaded type cast std::string operator uses minimal implementation instead of snprintf
1 parent eb9910c commit 3406ff2

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

drivers/CpuUid.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include <cstdio>
1817
#include "hal/cpu_uid_api.h"
1918
#include "drivers/CpuUid.h"
2019

2120
#if DEVICE_CPUUID
2221

2322
namespace mbed {
2423

24+
const char CpuUid::_hexChars[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
25+
2526
CpuUid::CpuUid() : _data(NULL)
2627
{
2728
_size = cpu_uid_get_length();
@@ -43,12 +44,11 @@ CpuUid::~CpuUid()
4344
CpuUid::operator std::string()
4445
{
4546
std::string str;
46-
char buf[3];
4747

4848
for (int i = 0; i < _size; ++i)
4949
{
50-
snprintf(buf, 3, "%.2X", _data[i]);
51-
str += buf;
50+
str += _hexChars[_data[i] >> 4];
51+
str += _hexChars[_data[i] & 0x0F];
5252
}
5353

5454
return str;

drivers/CpuUid.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class CpuUid {
8787
private:
8888
uint8_t * _data;
8989
int _size;
90+
91+
static const char _hexChars[16];
9092
};
9193

9294
} // namespace mbed

0 commit comments

Comments
 (0)