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 154
 }
154 155
 
155 156
 
156
-#define INT2STR_MAX_LEN 11 /* 10 digits + 0 */
157
+#define INT2STR_MAX_LEN  (19+1+1) /* 2^64~= 16*10^18 => 19+1 digits + \0 */
157 158
 
158 159
 /* returns a pointer to a static buffer containing l in asciiz & sets len */
159
-static inline char* int2str(unsigned int l, int* len)
160
+static inline char* int2str(unsigned long l, int* len)
160 161
 {
161 162
 	static char r[INT2STR_MAX_LEN];
162 163
 	int i;
163 164
 	
164
-	i=9;
165
-	r[10]=0; /* null terminate */
165
+	i=INT2STR_MAX_LEN-2;
166
+	r[INT2STR_MAX_LEN-1]=0; /* null terminate */
166 167
 	do{
167 168
 		r[i]=l%10+'0';
168 169
 		i--;
... ...
@@ -171,7 +172,7 @@ static inline char* int2str(unsigned int l, int* len)
171 172
 	if (l && (i<0)){
172 173
 		LOG(L_CRIT, "BUG: int2str: overflow\n");
173 174
 	}
174
-	if (len) *len=9-i;
175
+	if (len) *len=(INT2STR_MAX_LEN-2)-i;
175 176
 	return &r[i+1];
176 177
 }
177 178