diff options
author | Christian Cunningham <cc@localhost> | 2022-02-05 02:28:47 -0700 |
---|---|---|
committer | Christian Cunningham <cc@localhost> | 2022-02-05 02:28:47 -0700 |
commit | 3459ee85fc3ebeb28b426843b179d017160184f2 (patch) | |
tree | 6877fd5f0253bd8fa5d7924770ad69dab2c8f8fa /src | |
parent | 4ccbd3a14b90ad444f5a6fd099081d89a2ff98be (diff) |
Added data-to-string functions
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/strings.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/lib/strings.c b/src/lib/strings.c index 8b83c9f..53caaec 100644 --- a/src/lib/strings.c +++ b/src/lib/strings.c @@ -1,3 +1,4 @@ +#include <lib/kmem.h> #include <lib/strings.h> unsigned long strlen(string_t s) @@ -32,3 +33,77 @@ unsigned char strcmpn(string_t a, string_t b, unsigned int n) } return a[idx] == b[idx]; } + +char* zhex32_to_str(unsigned long value) +{ + char* data = kcalloc(9); + char tmp = 0; + char isz = -1; + for (int i = 0; i < 8; i++) { + tmp = (value >> 4*(8-i-1))&0xF; + if (isz == 0xFF && tmp != 0) + isz = i; + if(tmp > 0x9) + tmp += 7; + tmp += 0x30; + data[i] = tmp; + } + return data+isz; +} + +char* hex32_to_str(unsigned long value) +{ + char* data = kcalloc(9); + char tmp = 0; + for (int i = 0; i < 8; i++) { + tmp = (value >> 4*(8-i-1))&0xF; + if(tmp > 0x9) + tmp += 7; + tmp += 0x30; + data[i] = tmp; + } + return data; +} + +char* u32_to_str(unsigned long value) +{ + unsigned long t = value; + unsigned long c; + char* data = kcalloc(11); + char* dptr = data + 9; + for (int i = 0; i <= 10; i++) { + c = t%10; + *dptr = 0x30 + (c&0xF); + t /= 10; + if (t==0) + break; + dptr -= 1; + } + return dptr; +} + +char* s32_to_str(unsigned long value) +{ + long t = value; + unsigned long c; + char is_neg = 0; + if (t < 0) { + t = -t; + is_neg = 1; + } + char* data = kcalloc(12); + char* dptr = data + 10; + for (int i = 0; i <= 10; i++) { + c = t%10; + *dptr = 0x30 + (c&0xF); + t /= 10; + if (t==0) + break; + dptr -= 1; + } + if (is_neg) { + dptr -= 1; + *dptr = '-'; + } + return dptr; +} |