itoa function (or similar) in Linux

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 42
Reputation: marcosjp is an unknown quantity at this point 
Solved Threads: 0
marcosjp marcosjp is offline Offline
Light Poster

itoa function (or similar) in Linux

 
0
  #1
Sep 28th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 251
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: itoa function (or similar) in Linux

 
0
  #2
Sep 28th, 2008
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
"Any fool can know, point is to understand"
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: itoa function (or similar) in Linux

 
0
  #3
Sep 29th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1
Reputation: sladevi is an unknown quantity at this point 
Solved Threads: 0
sladevi sladevi is offline Offline
Newbie Poster

Re: itoa function (or similar) in Linux

 
0
  #4
Oct 21st, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: itoa function (or similar) in Linux

 
0
  #5
Oct 21st, 2008
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. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC