From b82e008d1883225931f1c34059eb7587d2f61b6e Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 24 Jun 2025 11:29:22 +0200 Subject: [PATCH] include/strutils: add ul_strtou16() Signed-off-by: Karel Zak --- include/strutils.h | 2 ++ lib/strutils.c | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/strutils.h b/include/strutils.h index 7cb365254..e462040c8 100644 --- a/include/strutils.h +++ b/include/strutils.h @@ -29,6 +29,8 @@ extern int ul_strtos64(const char *str, int64_t *num, int base); extern int ul_strtou64(const char *str, uint64_t *num, int base); extern int ul_strtos32(const char *str, int32_t *num, int base); extern int ul_strtou32(const char *str, uint32_t *num, int base); +extern int ul_strtou16(const char *str, uint16_t *num, int base); + extern int ul_strtold(const char *str, long double *num); extern int64_t str2num_or_err(const char *str, int base, const char *errmesg, int64_t low, int64_t up); diff --git a/lib/strutils.c b/lib/strutils.c index de0be7596..2316ada11 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -393,6 +393,19 @@ int ul_strtou32(const char *str, uint32_t *num, int base) return rc; } +int ul_strtou16(const char *str, uint16_t *num, int base) +{ + uint64_t tmp; + int rc; + + rc = ul_strtou64(str, &tmp, base); + if (rc == 0 && tmp > UINT16_MAX) + rc = -(errno = ERANGE); + if (rc == 0) + *num = (uint16_t) tmp; + return rc; +} + /* * Convert strings to numbers in defined range and print message on error. * -- 2.47.2