Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 1 | |
| 2 | #include <string.h> |
| 3 | #include <stdint.h> |
| 4 | #include <errno.h> |
| 5 | |
| 6 | #include <osmocore/utils.h> |
| 7 | |
| 8 | const char *get_value_string(const struct value_string *vs, uint32_t val) |
| 9 | { |
| 10 | int i; |
| 11 | |
| 12 | for (i = 0;; i++) { |
| 13 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 14 | break; |
| 15 | if (vs[i].value == val) |
| 16 | return vs[i].str; |
| 17 | } |
| 18 | return "unknown"; |
| 19 | } |
| 20 | |
| 21 | int get_string_value(const struct value_string *vs, const char *str) |
| 22 | { |
| 23 | int i; |
| 24 | |
| 25 | for (i = 0;; i++) { |
| 26 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 27 | break; |
| 28 | if (!strcasecmp(vs[i].str, str)) |
| 29 | return vs[i].value; |
| 30 | } |
| 31 | return -EINVAL; |
| 32 | } |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 33 | |
| 34 | char bcd2char(uint8_t bcd) |
| 35 | { |
| 36 | if (bcd < 0xa) |
| 37 | return '0' + bcd; |
| 38 | else |
| 39 | return 'A' + (bcd - 0xa); |
| 40 | } |
| 41 | |
| 42 | /* only works for numbers in ascci */ |
| 43 | uint8_t char2bcd(char c) |
| 44 | { |
| 45 | return c - 0x30; |
| 46 | } |