tags:

views:

7241

answers:

7

itoa() is a really handy function to convert a number to a string. Linux does not seem to have itoa(), is there an equivalent function or do I have to use sprintf(str, "%d", num) ?

+6  A: 

EDIT: Sorry, I should have remembered that this machine is decidedly non-standard, having plugged in various non-standard libc implementations for academic purposes ;-)

As itoa() is indeed non-standard, as mentioned by several helpful commenters, it is best to use sprintf(target_string,"%d",source_int) or (better yet, because it's safe from buffer overflows) snprintf(target_string, size_of_target_string_in_bytes, "%d", source_int). I know it's not quite as concise or cool as itoa(), but at least you can Write Once, Run Everywhere (tm) ;-)

Here's the old (edited) answer

You are correct in stating that the default gcc libc does not include itoa(), like several other platforms, due to it not technically being a part of the standard. See here for a little more info. Note that you have to

#include <stdlib.h>

Of course you already know this, because you wanted to use itoa() on Linux after presumably using it on another platform, but... the code (stolen from the link above) would look like:

Example

/* itoa example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i;
  char buffer [33];
  printf ("Enter a number: ");
  scanf ("%d",&i);
  itoa (i,buffer,10);
  printf ("decimal: %s\n",buffer);
  itoa (i,buffer,16);
  printf ("hexadecimal: %s\n",buffer);
  itoa (i,buffer,2);
  printf ("binary: %s\n",buffer);
  return 0;
}

Output:

Enter a number: 1750
decimal: 1750
hexadecimal: 6d6
binary: 11011010110

Hope this helps!

Matt J
Hmmm, compiling that on Debian gives me "undefined reference to `itoa'". Maybe something is wrong with my system.
Adam Pierce
I get the same on Ubuntu 8.04. I can find no reference to itoa in stdio.h or stdlib.h either (not suprising since it is not part of the standard)
camh
That is non-standard.
Paul Nathan
edited for correctness, thanks guys! sorry, I always forget that this isn't a vanilla Linux box ;-)
Matt J
your snprintf argument order is wrong.
Brian Gianforcaro
I have edited the answer to include the buffer size argument; I believe everything is as it should be now, I don't see a problem with the order of the arguments per se. Am I missing something?
Matt J
+2  A: 

As Matt J wrote, there is an itoa, but it's not standard. Your code will be more portable if you use snprintf.

A: 

Write your own; it's non-standard.

Paul Nathan
A: 

[off topic] based on your description, a free warning: choose snprintf instead of sprintf ...

PW
A: 

I have used _itoa(...) on RedHat 6 and GCC compiler. It works.

m_pGladiator
+3  A: 

If you are calling it a lot, the advice of "just use snprintf" can be annoying. So here's what you probably want:

const char *my_itoa_buf(char *buf, size_t len, int num)
{
  static char loc_buf[sizeof(int) * CHAR_BITS]; /* not thread safe */

  if (!buf)
  {
    buf = loc_buf;
    len = sizeof(loc_buf);
  }

  if (snprintf(buf, len, "%d", num) == -1)
    return ""; /* or whatever */

  return buf;
}

const char *my_itoa(int num)
{ return my_itoa_buf(NULL, 0, num); }
James Antill
Keep in mind this static buffer won't be threadsafe.
bdonlan
like the comment said :)
James Antill
A: 

you can use: http://blog.csdn.net/wwwsq/archive/2010/01/05/5137006.aspx

it's the best and fastest.

wsq