Thread: itoa

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    Smile itoa

    I have spent hours on this program and cannot get it to produce negatives or INT_MIN. Here is my code. Any help would be so appreciated I can't even begin to tell you...

    Code:
    #include <stdio.h>
    #include <iostream>
    using namespace std; 
    
    char *strrev(char *);
    char *itoa_3(int, char *, int);
    
    int main()
     {
      char A[40]; 
    
      cout << "cout base 10" << endl; 
      cout << 17 << endl; 
      cout << 0 << endl; 
      cout << -17 << endl; 
      cout << INT_MAX << endl; 
      cout << INT_MIN << endl; 
    
      cout << "\ncout base 16" << endl; 
      cout << hex << 17 << endl; 
      cout << 0 << endl; 
      cout << -17 << endl; 
      cout << INT_MAX << endl; 
      cout << INT_MIN << endl; 
      cout << dec; 
    
      cout << "\nitoa_3 base 2" << endl; 
      cout << itoa_3(17, A, 2) << endl; 
      cout << itoa_3(0, A, 2) << endl; 
      cout << itoa_3(-17, A, 2) << endl; 
      cout << itoa_3(INT_MAX, A, 2) << endl; 
      cout << itoa_3(INT_MIN, A, 2) << endl; 
    
      cout << "\nitoa_3 base 10" << endl; 
      cout << itoa_3(17, A, 10) << endl; 
      cout << itoa_3(0, A, 10) << endl; 
      cout << itoa_3(-17, A, 10) << endl; 
      cout << itoa_3(INT_MAX, A, 10) << endl; 
      cout << itoa_3(INT_MIN, A, 10) << endl; 
    
      cout << "\nitoa_3 base 16" << endl; 
      cout << itoa_3(17, A, 16) << endl; 
      cout << itoa_3(0, A, 16) << endl; 
      cout << itoa_3(-17, A, 16) << endl; 
      cout << itoa_3(INT_MAX, A, 16) << endl; 
      cout << itoa_3(INT_MIN, A, 16) << endl; 
    
      cout << "\nitoa_3 base 32" << endl; 
      cout << itoa_3(17, A, 32) << endl; 
      cout << itoa_3(0, A, 32) << endl; 
      cout << itoa_3(-17, A, 32) << endl; 
      cout << itoa_3(INT_MAX, A, 32) << endl; 
      cout << itoa_3(INT_MIN, A, 32) << endl; 
    
      cout << "\nitoa_3 base 36" << endl; 
      cout << itoa_3(17, A, 36) << endl; 
      cout << itoa_3(0, A, 36) << endl; 
      cout << itoa_3(-17, A, 36) << endl; 
      cout << itoa_3(INT_MAX, A, 36) << endl; 
      cout << itoa_3(INT_MIN, A, 36) << endl; 
    
      return 0; 
    } 
    
    char *strrev(char *str) {
    	char *p1, *p2;
    
    	if (!str || !*str)
    		return str;
    
    	for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) {
    		*p1 ^= *p2;
    		*p2 ^= *p1;
    		*p1 ^= *p2;
    	}
    
    	return str;
    }
    
    char *itoa_3(int n, char *s, int b) {
    	static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
    	int i=0, sign;
        
    	if ((sign = n) < 0)
    		n = -n;
    
    	do {
    		s[i++] = digits[n % b];
    	} while ((n /= b) != 0);
    
    	if (sign < 0)
    		s[i++] = '-';
    	s[i] = '\0';
    
    	return strrev(s);
    }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Washington
    Posts
    18
    Quote Originally Posted by BeanieRaven
    I have spent hours on this program and cannot get it to produce negatives or INT_MIN. Here is my code. Any help would be so appreciated I can't even begin to tell you...

    Code:
    #include <stdio.h>
    #include <iostream>
    using namespace std; 
    
    char *strrev(char *);
    char *itoa_3(int, char *, int);
    
    int main()
     {
      char A[40]; 
    
      cout << "cout base 10" << endl; 
      cout << 17 << endl; 
      cout << 0 << endl; 
      cout << -17 << endl; 
      cout << INT_MAX << endl; 
      cout << INT_MIN << endl; 
    
      cout << "\ncout base 16" << endl; 
      cout << hex << 17 << endl; 
      cout << 0 << endl; 
      cout << -17 << endl; 
      cout << INT_MAX << endl; 
      cout << INT_MIN << endl; 
      cout << dec; 
    
      cout << "\nitoa_3 base 2" << endl; 
      cout << itoa_3(17, A, 2) << endl; 
      cout << itoa_3(0, A, 2) << endl; 
      cout << itoa_3(-17, A, 2) << endl; 
      cout << itoa_3(INT_MAX, A, 2) << endl; 
      cout << itoa_3(INT_MIN, A, 2) << endl; 
    
      cout << "\nitoa_3 base 10" << endl; 
      cout << itoa_3(17, A, 10) << endl; 
      cout << itoa_3(0, A, 10) << endl; 
      cout << itoa_3(-17, A, 10) << endl; 
      cout << itoa_3(INT_MAX, A, 10) << endl; 
      cout << itoa_3(INT_MIN, A, 10) << endl; 
    
      cout << "\nitoa_3 base 16" << endl; 
      cout << itoa_3(17, A, 16) << endl; 
      cout << itoa_3(0, A, 16) << endl; 
      cout << itoa_3(-17, A, 16) << endl; 
      cout << itoa_3(INT_MAX, A, 16) << endl; 
      cout << itoa_3(INT_MIN, A, 16) << endl; 
    
      cout << "\nitoa_3 base 32" << endl; 
      cout << itoa_3(17, A, 32) << endl; 
      cout << itoa_3(0, A, 32) << endl; 
      cout << itoa_3(-17, A, 32) << endl; 
      cout << itoa_3(INT_MAX, A, 32) << endl; 
      cout << itoa_3(INT_MIN, A, 32) << endl; 
    
      cout << "\nitoa_3 base 36" << endl; 
      cout << itoa_3(17, A, 36) << endl; 
      cout << itoa_3(0, A, 36) << endl; 
      cout << itoa_3(-17, A, 36) << endl; 
      cout << itoa_3(INT_MAX, A, 36) << endl; 
      cout << itoa_3(INT_MIN, A, 36) << endl; 
    
      return 0; 
    } 
    
    char *strrev(char *str) {
    	char *p1, *p2;
    
    	if (!str || !*str)
    		return str;
    
    	for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) {
    		*p1 ^= *p2;
    		*p2 ^= *p1;
    		*p1 ^= *p2;
    	}
    
    	return str;
    }
    
    char *itoa_3(int n, char *s, int b) {
    	static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
    	int i=0, sign;
        
    	if ((sign = n) < 0)
    		n = -n;
    
    	do {
    		s[i++] = digits[n % b];
    	} while ((n /= b) != 0);
    
    	if (sign < 0)
    		s[i++] = '-';
    	s[i] = '\0';
    
    	return strrev(s);
    }
    WTF are you trying to accomplish?

    Code:
    #include <stdio.h>
    #include <string.h> /* for memset */
    
    void swap( char* i, char* j )
    {
        char c = *i;
        *i = *j;
        *j = c;
    }
    
    void reverse( char* str )
    {
        char* i;
        char* j;
        for( j = str; *(j + 1); j++ );  /* get j to point at the last char in the str */
        for( i = str; i < j; i++, j-- ) /* swap i (first++) with j (last--) */
        {
    	swap( i, j );
        }
    }
    
    void itoa( int n, char* str )
    {
        int i;
        int sign;
    
        if( (sign = n) < 0 ) /* is n positive or negative */
        {
    	n = -n; /* reverse the sign so that we work with a positive integer */
        }
        i = 0;
        do
        {
    	str[i++] = n % 10 + '0'; /* add the ascii offset to the digit value */
        } while( (n /= 10) > 0 );
        if( sign < 0 )
        {
            str[i++] = '-'; /* put the sign back on */
        }
        reverse( str );  /* put the string in proper reading order */
    }
    
    int main()
    {
        int i = 14593;
        int j = -84930;
        char buffer[BUFSIZ] = {0};
    
        itoa( i, buffer );
        printf( "%s\n", buffer );
        memset( buffer, 0, sizeof( buffer ) / sizeof( char ) );
        
        itoa( j, buffer );
        printf( "%s\n", buffer );
        
        return 0;
    }

    :davis:

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    I am trying to get output that looks like this:

    cout base 10
    17
    0
    -17
    2147483647
    -2147483648

    cout base 16
    11
    0
    ffffffef
    7fffffff
    80000000

    itoa_3 base 2
    10001
    0
    11111111111111111111111111101111
    1111111111111111111111111111111
    10000000000000000000000000000000

    itoa_3 base 10
    17
    0
    -17
    2147483647
    -2147483648

    itoa_3 base 16
    11
    0
    ffffffef
    7fffffff
    80000000

    itoa_3 base 32
    h
    0
    3vvvvvf
    1vvvvvv
    2000000

    itoa_3 base 36
    h
    0
    1z141yn
    zik0zj
    zik0zk

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Something like this may work:
    Code:
    #include <iostream>
    #include <climits>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
       int size[] = {2,8,10,16,32,36};
       for ( size_t j = 0; j < sizeof size / sizeof *size; ++j)
       {
          const int array[] = {17,0,-17,INT_MAX,INT_MIN};
          char buffer[40];
          cout << "itoa base " << size[j] << ":\n"; 
          for ( size_t i = 0; i < sizeof array / sizeof *array; ++i )
          {
             cout << itoa(array[i], buffer, size[j]) << '\n';
          }
       }
    
       return 0;
    }
    But itoa is non-standard.

    Or are you trying to prove out your itoa?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    Thanks Dave. I am trying to prove my version of itoa, but yours is producing the results I'm looking. I am sure to learn from it and solve my problem. Thank you again!

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Then this thread may be of interest.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using itoa
    By g_p in forum C Programming
    Replies: 2
    Last Post: 05-03-2008, 06:38 AM
  2. Problem with itoa() perhaps?
    By TheSquid in forum C++ Programming
    Replies: 5
    Last Post: 05-08-2006, 02:04 AM
  3. Really Weird itoa Problem
    By Grantyt3 in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 12:44 AM
  4. itoa
    By coldcoyote in forum Linux Programming
    Replies: 4
    Last Post: 02-13-2003, 09:28 AM
  5. itoa
    By Thantos in forum C Programming
    Replies: 2
    Last Post: 09-18-2001, 02:23 PM