Solved

itoa function (not atoi)

Posted on 1997-09-08
5
2,016 Views
Last Modified: 2006-11-17
I'm looking for a standard function to do integer-to-string conversion.  The comp.lang.c FAQ says to just use sprintf, but I'd rather live without that cruft.  I could swear I found just such a function once while browsing through man pages, but can't locate it again.
0
Comment
Question by:dhm
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
msmits earned 50 total points
ID: 1254305
Depends on your platform I guess. My borland compiler supports the following function:

  char *itoa(int value, char *string, int radix);

It is documented in the DOS reference, but mentions portability to the win16, win32 and OS/2 platforms. It also says that it is not standard available in ANSI C, ANSI C++ and UNIX.

0
 
LVL 3

Author Comment

by:dhm
ID: 1254306
Oops, I should've specified unix.  I guess I'll have to ask again.  Thanks for the reply.
0
 
LVL 3

Expert Comment

by:msmits
ID: 1254307
After consulting a UNIX Sun Solaris system I came up with the following two from the atoi(3) man page:

  char *lltostr(long long value, char *ptr);
  char *ulltostr(unsigned long long value, char *ptr);

Otherwise a standard itoa is not very difficult:

  char *itoa(int val, char *ptr)
  {
    int sign = (val < 0) ? -1 : 1;
    char tmp[16];
    int i = 0, j = 0;

    val = sign * val;
    while (val != 0) {
      tmp[i] = (val % 10) + '0';
      val = val / 10;
      i++;
    }
    if (i == 0) {
      strcpy(ptr, "0");
    } else {
      if (sign == -1) {
        ptr[j] = '-';
        j++;
      }
      while (i != 0) {
        ptr[j] = tmp[i - 1];
        i--;
        j++;
      }
      ptr[j] = '\000';
    }
  }

While this may not be the most efficient solution or it may not even work, as I completely did this without a compiler and some test cases, it shows that the base-10 algorithm is not difficult.

Although I agree that a library function would be better, so you don't have to invent and test such trivial stuff...

0
 
LVL 3

Author Comment

by:dhm
ID: 1254308
Thanks for snooping around...I'd found the Solaris functions too, but they don't exist on the other platforms I compile for.  I ended up just writing itoa, ltoa, utoa, and ultoa:

const char *itoa( int n )
{
      static char buf[32]; /* yeech */
      sprintf( buf, "%d", n );
      return buf;
}

Now I've just gotta find out how to do thread-specific static
function data (on all those platforms).  *That* sort of thing is the hard part on library functions like this.  The logic is, as you say, trivial.
0
 
LVL 3

Expert Comment

by:msmits
ID: 1254309
There are several solutions:
1. require the caller to pass a valid character buffer

2. malloc the space and require the caller to free it

I find the first solution the 'most' clean.

0

Featured Post

Free book by J.Peter Bruzzese, Microsoft MVP

Are you using Office 365? Trying to set up email signatures but you’re struggling with transport rules and connectors? Let renowned Microsoft MVP J.Peter Bruzzese show you how in this exclusive e-book on Office 365 email signatures. Better yet, it’s free!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now