Browse code

- int2str updated: works for 64bits longs, minor cleanup

Andrei Pelinescu-Onciul authored on 08/03/2004 13:12:48
Showing 1 changed files
... ...
@@ -36,6 +36,7 @@
36 36
  * 2003-02-28 scratchpad compatibility abandoned (jiri)
37 37
  * 2003-03-30 str2int and str2float added (janakj)
38 38
  * 2003-04-26 ZSW (jiri)
39
+ * 2004-03-08 updated int2str (64 bits, INT2STR_MAX_LEN used) (andrei)
39 40
  */
40 41
 
41 42
 
... ...
@@ -153,16 +154,16 @@ static inline int btostr( char *p,  unsigned char val)
153 153
 }
154 154
 
155 155
 
156
-#define INT2STR_MAX_LEN 11 /* 10 digits + 0 */
156
+#define INT2STR_MAX_LEN  (19+1+1) /* 2^64~= 16*10^18 => 19+1 digits + \0 */
157 157
 
158 158
 /* returns a pointer to a static buffer containing l in asciiz & sets len */
159
-static inline char* int2str(unsigned int l, int* len)
159
+static inline char* int2str(unsigned long l, int* len)
160 160
 {
161 161
 	static char r[INT2STR_MAX_LEN];
162 162
 	int i;
163 163
 	
164
-	i=9;
165
-	r[10]=0; /* null terminate */
164
+	i=INT2STR_MAX_LEN-2;
165
+	r[INT2STR_MAX_LEN-1]=0; /* null terminate */
166 166
 	do{
167 167
 		r[i]=l%10+'0';
168 168
 		i--;
... ...
@@ -171,7 +172,7 @@ static inline char* int2str(unsigned int l, int* len)
171 171
 	if (l && (i<0)){
172 172
 		LOG(L_CRIT, "BUG: int2str: overflow\n");
173 173
 	}
174
-	if (len) *len=9-i;
174
+	if (len) *len=(INT2STR_MAX_LEN-2)-i;
175 175
 	return &r[i+1];
176 176
 }
177 177