Basically for part of my assignment, I have to write my own int to string. This is done, however the problem is I can only enter 6 digits to convert otherewise my program crashes. Out lecturer also wants us to use the abs function as the value passed in can be negative. When i use the abs fucntion, I can only enter 5 digits.

My function is defined as:

char *IntToStr(long Val, char *Str);

Val is the number passed in and *Str is what it is returned in.

I think it's something to do with memory but I'm not too sure.

Any help appreciated.

Thanks, Ian

It's somewhat difficult to tell you what's wrong with the code when we can't see the code. :icon_rolleyes:

char *IntToStr(long Val, char *Str)
{
   int counter = 0;				
   int endofarray = 0;
   int holder = 0;
   int ExitLoop = 0;
   char temp;


   for(counter = 0; counter < 6; counter++)	
   {

     if(Val != 0)				
     {
       holder = (Val%10 + 48);			
       Str[counter] = holder;			
       Val = Val/10;			
       endofarray ++;				
     }
     else
     {
       Str[counter] = '\0';			
     }
   }

   holder = endofarray/2;			
   ExitLoop= endofarray%2;
   ExitLoop = ExitLoop + holder;

   for(counter = 0; counter < 6; counter++)	
   {
      temp = Str[counter];			
      Str[counter] = Str[endofarray-1];		
      Str[endofarray-1] = temp;			

      endofarray--;				
      ExitLoop--;				

      if(ExitLoop<=0)				
      {
         counter = 5;				
      }
   }
}

>for(counter = 0; counter < 6; counter++)
This is a little unusual. You're converting an int, but an int isn't necessarily 5 characters (remember to make room for the null character at the end of the string). You should be using the digits in the value to drive the loop. That way you can handle integers of any length. Right now if the value doesn't reach 0 before the loop ends, you don't terminate the string with a null character.

>holder = (Val%10 + 48);
Instead of 48 you should be using '0'. Magic numbers are a very bad idea.

The idea is to loop through the digits in the number and add them to the string. If you want to handle signed values, save whether the original value is negative first (because it'll be 0 when you're done working with it). You also need to take into account the actual value 0. After the loop, append a '-' character if the value was negative originally, then append a '\0' character. The number was built in reverse, so you need to reverse it too. I'd expect a valid solution to look like this:

char *IntToStr ( long Val, char *Str )
{
  if ( Val == 0 ) {
    Str[0] = '0';
    Str[1] = '\0';
  }
  else {
    int sign = 0;
    int end = 0;
    int start = 0;

    if ( Val < 0 )
      sign = 1;

    while ( Val != 0 ) {
      Str[end] = (char)( abs ( Val % 10 ) + '0' );
      Val /= 10;
      ++end;
    }

    if ( sign )
      Str[end++] = '-';

    Str[end] = '\0';

    /* The number was build in reverse; fix it */
    while ( start < end ) {
      char temp = Str[start];
      Str[start++] = Str[--end];
      Str[end] = temp;
    }
  }

  return Str;
}

When your teacher said to use abs, he probably meant to remove the sign from the original value before you process it:

Val = abs ( Val );

/* Run the processing loop */

But that's actually a broken solution for two's complement because you won't be able to successfully convert the most negative value (INT_MIN). So instead you should leave the sign intact and use abs on the extracted digit as shown in this line:

Str[end] = (char)( abs ( Val % 10 ) + '0' );

Thanks alot for your help Narue, sorted me out =D

This article has been dead for over six months. Start a new discussion instead.