diff --git a/src/modules/_Roxen/roxen.c b/src/modules/_Roxen/roxen.c
index 304b78195a0e659c17448bda3272943ed84294e8..f75b0f24f5d8ac709d6c7dd28cdda8c66bd537bf 100644
--- a/src/modules/_Roxen/roxen.c
+++ b/src/modules/_Roxen/roxen.c
@@ -487,33 +487,6 @@ static void f_html_encode_string( INT32 args )
     void o_cast_to_string();
 
     case PIKE_T_INT:
-      /* Optimization, this is basically a inlined cast_int_to_string */
-      {
-	char buf[21], *b = buf+19;
-	int neg, i, j=0;
-	i = Pike_sp[-1].u.integer;
-	pop_stack();
-	if( i < 0 )
-	{
-	  neg = 1;
-	  i = -i;
-	}
-	else
-	  neg = 0;
-
-	buf[20] = 0;
-
-	while( i >= 10 )
-	{
-	  b[ -j++ ] = '0'+(i%10);
-	  i /= 10;
-	}
-	b[ -j++ ] = '0'+(i%10);
-	if( neg )  b[ -j++ ] = '-';
-	push_text( b-j+1 );
-      }
-      return;
-
     case PIKE_T_FLOAT:
       /* Optimization, no need to check the resultstring for
        * unsafe characters. 
diff --git a/src/operators.c b/src/operators.c
index e1db215e5d3071378260a4e91bad8b5a4ce76799..a325180f40ae1ffa29f5daa89c6ff3ea80bdac94 100644
--- a/src/operators.c
+++ b/src/operators.c
@@ -39,6 +39,10 @@
 #define OP_MODULO_BY_ZERO_ERROR(FUNC) \
      math_error(FUNC, sp-2, 2, 0, "Modulo by zero.\n")
 
+    /* These calculations should always give some margin based on the size. */
+    /* One extra char for the sign. */
+#define MAX_INT_SPRINTF_LEN (1 + (SIZEOF_INT_TYPE * 5 + 1) / 2)
+
 /* The destructive multiset merge code is broken.
  * l->msd gets -1 refs.
  *
@@ -329,7 +333,8 @@ PMOD_EXPORT void o_cast_to_int(void)
 /* Special case for casting to string. */
 PMOD_EXPORT void o_cast_to_string(void)
 {
-  char buf[MAX_NUM_BUF];
+  struct pike_string *s;
+
   switch(TYPEOF(sp[-1]))
   {
   case T_OBJECT:
@@ -357,7 +362,6 @@ PMOD_EXPORT void o_cast_to_string(void)
 			    LFUN__IS_TYPE);
 	  if( f != -1)
 	  {
-	    struct pike_string *s;
 	    REF_MAKE_CONST_STRING(s, "string");
 	    push_string(s);
 	    apply_low(o, f, 1);
@@ -376,7 +380,6 @@ PMOD_EXPORT void o_cast_to_string(void)
     {
       int i, alen;
       struct array *a = sp[-1].u.array;
-      struct pike_string *s;
       int shift = 0;
       alen = a->size;
 
@@ -440,9 +443,8 @@ PMOD_EXPORT void o_cast_to_string(void)
 	}
 	break;
       }
-      s = end_shared_string(s);
       pop_stack();
-      push_string(s);
+      push_string(end_shared_string(s));
     }
     return;
 
@@ -453,15 +455,44 @@ PMOD_EXPORT void o_cast_to_string(void)
     return;
 
   case T_FLOAT:
-    format_pike_float (buf, sp[-1].u.float_number);
-    break;
+    {
+      char buf[MAX_FLOAT_SPRINTF_LEN+1];
+      format_pike_float (buf, sp[-1].u.float_number);
+      s = make_shared_string(buf);
+      break;
+    }
 
   case T_INT:
-    sprintf(buf, "%"PRINTPIKEINT"d", sp[-1].u.integer);
+    {
+      INT_TYPE org;
+      char buf[MAX_INT_SPRINTF_LEN];
+      register char*b = buf+sizeof buf-1;
+      register unsigned INT_TYPE i;
+      org = sp[-1].u.integer;
+      *b-- = '\0';
+      i = org;
+      
+      if( org < 0 )
+        i = -i;
+      
+      goto jin;				       /* C as a macro assembler :-) */
+      do
+      {
+        i /= 10;
+jin:    *b-- = '0'+(i%10);
+      }
+      while( i >= 10 );
+      
+      if( org < 0 )
+        *b = '-';
+      else
+        b++;
+      s = make_shared_string(b);
+    }
     break;
   }
-	
-  SET_SVAL(sp[-1], PIKE_T_STRING, 0, string, make_shared_string(buf));
+
+  SET_SVAL(sp[-1], PIKE_T_STRING, 0, string, s);
 }
 
 PMOD_EXPORT void o_cast(struct pike_type *type, INT32 run_time_type)