Browse code

Use strutils instead of snprintf (to save some bytes), set default year to 2014, partial revamp of the stopwatch

Dario Rodriguez authored on 14/01/2014 22:59:59
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,45 @@
1
+int num2str(int num, char *str, unsigned int minlen, unsigned int maxlen)
2
+{
3
+        unsigned int i,d,n,k;
4
+        /* sanity check */
5
+        if(num<0 || minlen==0 || maxlen==0) {
6
+                *str='\0';
7
+                return(0);
8
+        }
9
+        /* safe method to get the divisor */
10
+        for(n=d=1,i=1;i<maxlen && !(d>1 && (d/n)!=10);i++,n=d,d*=10)
11
+                ;
12
+        if(d>1 && (d/n)!=10) {
13
+                i--;
14
+                d=n;
15
+        }
16
+        /* extract the digits */
17
+        for(k=0;d>0;i--,d/=10) {
18
+                n=(((unsigned int)num)/d)%10;
19
+                if(n!=0 || minlen>=i) {
20
+                        str[k++]=n+'0';
21
+                        minlen=i;
22
+                }
23
+        }
24
+        /* terminate string */
25
+        str[k]='\0';
26
+	return(k);
27
+}
28
+
29
+
30
+int hexnum2str(int hexnum, char *str, unsigned int numdigits)
31
+{
32
+	int k,s;
33
+	int c;
34
+	if(hexnum<0 || numdigits==0) {
35
+		*str='\0';
36
+		return(0);
37
+	}
38
+	for(k=0,s=(numdigits-1)*4;k<numdigits;k++,s-=4) {
39
+		c=(((unsigned int)hexnum)>>s)&0xf;
40
+		str[k]=((c<10)?(c+'0'):(c+'a'-10));
41
+	}
42
+	str[k]='\0';
43
+	return(k);
44
+}
45
+