diff options
Diffstat (limited to 'libosmocore/src/utils.c')
-rw-r--r-- | libosmocore/src/utils.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/libosmocore/src/utils.c b/libosmocore/src/utils.c new file mode 100644 index 000000000..2a73d397e --- /dev/null +++ b/libosmocore/src/utils.c @@ -0,0 +1,46 @@ + +#include <string.h> +#include <stdint.h> +#include <errno.h> + +#include <osmocore/utils.h> + +const char *get_value_string(const struct value_string *vs, uint32_t val) +{ + int i; + + for (i = 0;; i++) { + if (vs[i].value == 0 && vs[i].str == NULL) + break; + if (vs[i].value == val) + return vs[i].str; + } + return "unknown"; +} + +int get_string_value(const struct value_string *vs, const char *str) +{ + int i; + + for (i = 0;; i++) { + if (vs[i].value == 0 && vs[i].str == NULL) + break; + if (!strcasecmp(vs[i].str, str)) + return vs[i].value; + } + return -EINVAL; +} + +char bcd2char(uint8_t bcd) +{ + if (bcd < 0xa) + return '0' + bcd; + else + return 'A' + (bcd - 0xa); +} + +/* only works for numbers in ascci */ +uint8_t char2bcd(char c) +{ + return c - 0x30; +} |