Skip to content

Commit b31d3e7

Browse files
author
Scott Powell
committed
* added StrHelper::fromHex()
1 parent 1520f4d commit b31d3e7

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/helpers/TxtDataHelpers.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,23 @@ const char* StrHelper::ftoa(float f) {
139139
}
140140
return tmp;
141141
}
142+
143+
uint32_t StrHelper::fromHex(const char* src) {
144+
uint32_t n = 0;
145+
while (*src) {
146+
if (*src >= '0' && *src <= '9') {
147+
n <<= 4;
148+
n |= (*src - '0');
149+
} else if (*src >= 'A' && *src <= 'F') {
150+
n <<= 4;
151+
n |= (*src - 'A' + 10);
152+
} else if (*src >= 'a' && *src <= 'f') {
153+
n <<= 4;
154+
n |= (*src - 'a' + 10);
155+
} else {
156+
break; // non-hex char encountered, stop parsing
157+
}
158+
src++;
159+
}
160+
return n;
161+
}

src/helpers/TxtDataHelpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ class StrHelper {
1313
static void strzcpy(char* dest, const char* src, size_t buf_sz); // pads with trailing nulls
1414
static const char* ftoa(float f);
1515
static bool isBlank(const char* str);
16+
static uint32_t fromHex(const char* src);
1617
};

0 commit comments

Comments
 (0)