mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
fmt: add scn_u32_hex()
This commit is contained in:
parent
8c5ffa0a8d
commit
695a94449a
@ -460,6 +460,30 @@ uint32_t scn_u32_dec(const char *str, size_t n)
|
||||
return res;
|
||||
}
|
||||
|
||||
uint32_t scn_u32_hex(const char *str, size_t n)
|
||||
{
|
||||
uint32_t res = 0;
|
||||
|
||||
while (n--) {
|
||||
char c = *str++;
|
||||
if (!_is_digit(c)) {
|
||||
if (_is_upper(c)) {
|
||||
c = _to_lower(c);
|
||||
}
|
||||
if (c == '\0' || c > 'f') {
|
||||
break;
|
||||
}
|
||||
res <<= 4;
|
||||
res |= c - 'a' + 0xa;
|
||||
}
|
||||
else {
|
||||
res <<= 4;
|
||||
res |= c - '0';
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void print(const char *s, size_t n)
|
||||
{
|
||||
#ifdef __WITH_AVRLIBC__
|
||||
|
@ -376,6 +376,18 @@ size_t fmt_to_lower(char *out, const char *str);
|
||||
*/
|
||||
uint32_t scn_u32_dec(const char *str, size_t n);
|
||||
|
||||
/**
|
||||
* @brief Convert hexadecimal characters to uin32_t
|
||||
*
|
||||
* Will convert up to @p n char. Stop at any non-hexadecimal or '\0' character
|
||||
*
|
||||
* @param[in] str Pointer to tring to read from
|
||||
* @param[in] n Maximum number of characters to consider
|
||||
*
|
||||
* @return converted uint32_t value
|
||||
*/
|
||||
uint32_t scn_u32_hex(const char *str, size_t n);
|
||||
|
||||
/**
|
||||
* @brief Print string to stdout
|
||||
*
|
||||
|
@ -779,6 +779,17 @@ static void test_scn_u32_dec(void)
|
||||
TEST_ASSERT_EQUAL_INT(val2, scn_u32_dec(string1, 5));
|
||||
}
|
||||
|
||||
static void test_scn_u32_hex(void)
|
||||
{
|
||||
const char *string1 = "aB12cE4F";
|
||||
uint32_t val1 = 0xab12ce4f;
|
||||
uint32_t val2 = 0xab1;
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(val1, scn_u32_hex(string1, 8));
|
||||
TEST_ASSERT_EQUAL_INT(val2, scn_u32_hex(string1, 3));
|
||||
TEST_ASSERT_EQUAL_INT(val1, scn_u32_hex(string1, 9));
|
||||
}
|
||||
|
||||
static void test_fmt_lpad(void)
|
||||
{
|
||||
const char base[] = "abcd";
|
||||
@ -840,6 +851,7 @@ Test *tests_fmt_tests(void)
|
||||
new_TestFixture(test_fmt_char),
|
||||
new_TestFixture(test_fmt_to_lower),
|
||||
new_TestFixture(test_scn_u32_dec),
|
||||
new_TestFixture(test_scn_u32_hex),
|
||||
new_TestFixture(test_fmt_lpad),
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user