convertion of negative integers to string without using atoi function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • batista
    New Member
    • Sep 2014
    • 1

    convertion of negative integers to string without using atoi function

    char intToStr(int a)
    {
    int n, i, j, sign, set;
    char r[10], s[10];

    if (a[0] == '-')
    sign = -1;
    if (sign == -1)
    {
    i = 1;
    n = a % 10;
    s[i++] = n | '0';
    a = a / 10;
    }
    else
    {
    i = 0;
    n = a % 10;
    s[i++] = n | '0';
    a = a / 10;
    }
    i--;

    while (i >= 0)
    {
    r[j++] = s[i--];
    }
    return r;
    }
    i m the beginer of c programing language
    i have doubt at the time of handling of negative numbers at the time of covertng to string
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Negative numbers are written in 2's complement format. They are not formatted like positive numbers with a sign bit set.

    You get the 2's complement by reversing the bits an adding 1. Here is the bit pattern for 5:

    00000101

    Now reverse the bits:
    11111010

    and add 1:

    11111010
    00000001
    11111011 <- 2's complement for -5

    To convert -5 to plus 5 you reverse the bits and add 1:

    00000100
    00000001
    ---------
    00000101 <- +5

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      You say you want to write a function that converts an integer into a string, but you refer to atoi (which converts a string to an integer). Which way do you want the conversion to go?

      2's complement is by far the most common encoding technique, but others are permitted by the C Standard. If we assume that your function works properly for positive integers, then the following snippet handles almost all negative numbers properly. (It looks like you are sort of trying to do something like this.)
      Code:
      if (value < 0)
         return intToStr(-value);
      else
         return intToStr(value);
      However, it fails for negative numbers where -value cannot be represented as a positive number (eg, |INT_MIN| > INT_MAX, as is typically the case for 2's complement). This is a corner case that can be hard to handle.

      However, there are problems with intToStr:
      1. The function is defined to return a char, but it actually returns a pointer to char.
      2. The function returns a pointer to automatic variable r for use outside the scope of r.
      3. It tests a[0] as if a were a char pointer, but a is actually an int.
      4. sign is undefined if a[0] is not '-'.
      5. The function only converts a single decimal digit of the input.
      6. In the final loop that copies s to r, j is undefined.
      7. Converting integer digit to string digit via n | '0' will work for ASCII string encoding but could fail for other string encodings. The traditional way to do this is via n + '0'.
      8. There are more problems, but these are a good start.

      Comment

      Working...