943,559 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 33024
  • C RSS
Sep 28th, 2008
0

itoa function (or similar) in Linux

Expand Post »
Hello there!

How can I convert an INT value into a string?
I used the itoa function successfully in Windows:

  1. itoa(the_int_number, the_string, 10);

It worked fine in Windows (DevC++) but Linux did not recognize this function.

Does anyone know another way of converting INT into char[n] in C that Linux could accept?

Thank you for any hints!

Marcos
Reputation Points: 10
Solved Threads: 0
Light Poster
marcosjp is offline Offline
42 posts
since Mar 2008
Sep 28th, 2008
0

Re: itoa function (or similar) in Linux

Use sprintf function to convert int to string. Thats the more portable way of doing it. Since itoa is not portable!

  1. char *cvtInt( char *str, int num)
  2. {
  3. sprintf( str, "%d", num );
  4. }

ssharish
Reputation Points: 73
Solved Threads: 20
Posting Whiz in Training
ssharish2005 is offline Offline
253 posts
since Dec 2006
Sep 29th, 2008
0

Re: itoa function (or similar) in Linux

The sprintf does not implement itoa functionality (radix in 2..36 range).
Fortunately, it's so simple to get itoa portable solution:
  1. /* The Itoa code is in the puiblic domain */
  2. char* Itoa(int value, char* str, int radix) {
  3. static char dig[] =
  4. "0123456789"
  5. "abcdefghijklmnopqrstuvwxyz";
  6. int n = 0, neg = 0;
  7. unsigned int v;
  8. char* p, *q;
  9. char c;
  10.  
  11. if (radix == 10 && value < 0) {
  12. value = -value;
  13. neg = 1;
  14. }
  15. v = value;
  16. do {
  17. str[n++] = dig[v%radix];
  18. v /= radix;
  19. } while (v);
  20. if (neg)
  21. str[n++] = '-';
  22. str[n] = '\0';
  23. for (p = str, q = p + n/2; p != q; ++p, --q)
  24. c = *p, *p = *q, *q = c;
  25. return str;
  26. }
Last edited by ArkM; Sep 29th, 2008 at 3:03 am.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Oct 21st, 2008
0

Re: itoa function (or similar) in Linux

That's handy ArkM, but could you provide some references? How do you know the code is in the public domain and where did you get it from?

Also, is it just me or is that code bugged? When n/2 is odd, the for loop continues infinitely as p and q simply bypass each other, so the condition should actually be p<q, right?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sladevi is offline Offline
1 posts
since Oct 2008
Oct 21st, 2008
0

Re: itoa function (or similar) in Linux

Ohh, that's mea culpa... See right code below...
I know that this code is in the public domain because I have wrote it for 5 (or 10) minutes 22 days ago. So I have got it directly from my VC++ 2008 solution...
  1. /* The Itoa code is in the public domain */
  2. char* Itoa(int value, char* str, int radix) {
  3. static char dig[] =
  4. "0123456789"
  5. "abcdefghijklmnopqrstuvwxyz";
  6. int n = 0, neg = 0;
  7. unsigned int v;
  8. char* p, *q;
  9. char c;
  10.  
  11. if (radix == 10 && value < 0) {
  12. value = -value;
  13. neg = 1;
  14. }
  15. v = value;
  16. do {
  17. str[n++] = dig[v%radix];
  18. v /= radix;
  19. } while (v);
  20. if (neg)
  21. str[n++] = '-';
  22. str[n] = '\0';
  23.  
  24. for (p = str, q = p + (n-1); p < q; ++p, --q)
  25. c = *p, *p = *q, *q = c;
  26. return str;
  27. }
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: HELP!!!!
Next Thread in C Forum Timeline: Reading Lines from Text File





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC