diff options
-rw-r--r-- | include/osmocore/utils.h | 1 | ||||
-rw-r--r-- | src/utils.c | 30 |
2 files changed, 31 insertions, 0 deletions
diff --git a/include/osmocore/utils.h b/include/osmocore/utils.h index 93cefbe6..da4eb959 100644 --- a/include/osmocore/utils.h +++ b/include/osmocore/utils.h @@ -20,6 +20,7 @@ uint8_t char2bcd(char c); int hexparse(const char *str, uint8_t *b, int max_len); char *hexdump(const unsigned char *buf, int len); char *hexdump_nospc(const unsigned char *buf, int len); +char *bitdump(const uint8_t *bits, unsigned int len); #define static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1]; diff --git a/src/utils.c b/src/utils.c index 405039f6..52007931 100644 --- a/src/utils.c +++ b/src/utils.c @@ -95,6 +95,36 @@ static char *_hexdump(const unsigned char *buf, int len, char *delim) return hexd_buff; } +char *bitdump(const uint8_t *bits, unsigned int len) +{ + int i; + + if (len > sizeof(hexd_buff)-1) + len = sizeof(hexd_buff)-1; + memset(hexd_buff, 0, sizeof(hexd_buff)); + + for (i = 0; i < len; i++) { + char outch; + switch (bits[i]) { + case 0: + outch = '0'; + break; + case 0xff: + outch = '?'; + break; + case 1: + outch = '1'; + break; + default: + outch = 'E'; + break; + } + hexd_buff[i] = outch; + } + hexd_buff[sizeof(hexd_buff)-1] = 0; + return hexd_buff; +} + char *hexdump(const unsigned char *buf, int len) { return _hexdump(buf, len, " "); |