fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
fmt.c
Go to the documentation of this file.
1#include "fmt.h"
2
10unsigned int fmt_str(char *s,const char *t)
11{
12 unsigned int len;
13 char ch;
14 len = 0;
15 if (s) { while ((ch = t[len])) s[len++] = ch; }
16 else while (t[len]) len++;
17 return len;
18}
19
20unsigned int fmt_strn(char *s,const char *t,unsigned int n)
21{
22 unsigned int len;
23 char ch;
24 len = 0;
25 if (s) { while (n-- && (ch = t[len])) s[len++] = ch; }
26 else while (n-- && t[len]) len++;
27 return len;
28}
29
30unsigned int fmt_uint(char *s,unsigned int u)
31{
32 unsigned long l; l = u; return fmt_ulong(s,l);
33}
34
35unsigned int fmt_uint0(char *s,unsigned int u,unsigned int n)
36{
37 unsigned int len;
38 len = fmt_uint(FMT_LEN,u);
39 while (len < n) { if (s) *s++ = '0'; ++len; }
40 if (s) fmt_uint(s,u);
41 return len;
42}
43
44unsigned int fmt_ulong(char *s,unsigned long u)
45{
46 unsigned int len;
47 unsigned long q;
48 len = 1; q = u;
49 while (q > 9) { ++len; q /= 10; }
50 if (s) {
51 s += len;
52 do { *--s = '0' + (u % 10); u /= 10; } while(u); /* handles u == 0 */
53 }
54 return len;
55}
56
57unsigned int fmt_xlong(char *s,unsigned long u)
58{
59 unsigned int len;
60 unsigned long q;
61 len = 1; q = u;
62 while (q > 15) { ++len; q /= 16; }
63 if (s) {
64 s += len;
65 do { *--s = tohex(u % 16); u /= 16; } while(u); /* handles u == 0 */
66 }
67 return len;
68}
69
70char tohex(char num) {
71 if (num < 10)
72 return num + '0';
73 else if (num < 16)
74 return num - 10 + 'a';
75 else
76 return -1;
77}
78
79int fromhex(unsigned char c) {
80 if (c >= '0' && c <= '9')
81 return c-'0';
82 else if (c >= 'A' && c <= 'F')
83 return c -'A' + 10;
84 else if (c >= 'a' && c <= 'f')
85 return c - 'a' + 10;
86 return -1;
87}
unsigned int fmt_uint(char *s, unsigned int u)
Definition: fmt.c:30
unsigned int fmt_uint0(char *s, unsigned int u, unsigned int n)
Definition: fmt.c:35
unsigned int fmt_ulong(char *s, unsigned long u)
Definition: fmt.c:44
unsigned int fmt_str(char *s, const char *t)
Definition: fmt.c:10
int fromhex(unsigned char c)
Definition: fmt.c:79
unsigned int fmt_xlong(char *s, unsigned long u)
Definition: fmt.c:57
char tohex(char num)
Definition: fmt.c:70
unsigned int fmt_strn(char *s, const char *t, unsigned int n)
Definition: fmt.c:20
conversion function declarations
#define FMT_LEN
Definition: fmt.h:12