| | |
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:
Solved Threads: 0
Hello there!
How can I convert an INT value into a string?
I used the itoa function successfully in Windows:
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
How can I convert an INT value into a string?
I used the itoa function successfully in Windows:
c Syntax (Toggle Plain Text)
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
Use sprintf function to convert int to string. Thats the more portable way of doing it. Since itoa is not portable!
ssharish
c Syntax (Toggle Plain Text)
char *cvtInt( char *str, int num) { sprintf( str, "%d", num ); }
ssharish
"Any fool can know, point is to understand"
The sprintf does not implement itoa functionality (radix in 2..36 range).
Fortunately, it's so simple to get itoa portable solution:
Fortunately, it's so simple to get itoa portable solution:
c Syntax (Toggle Plain Text)
/* The Itoa code is in the puiblic domain */ char* Itoa(int value, char* str, int radix) { static char dig[] = "0123456789" "abcdefghijklmnopqrstuvwxyz"; int n = 0, neg = 0; unsigned int v; char* p, *q; char c; if (radix == 10 && value < 0) { value = -value; neg = 1; } v = value; do { str[n++] = dig[v%radix]; v /= radix; } while (v); if (neg) str[n++] = '-'; str[n] = '\0'; for (p = str, q = p + n/2; p != q; ++p, --q) c = *p, *p = *q, *q = c; return str; }
Last edited by ArkM; Sep 29th, 2008 at 3:03 am.
•
•
Join Date: Oct 2008
Posts: 1
Reputation:
Solved Threads: 0
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?
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?
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...
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... c Syntax (Toggle Plain Text)
/* The Itoa code is in the public domain */ char* Itoa(int value, char* str, int radix) { static char dig[] = "0123456789" "abcdefghijklmnopqrstuvwxyz"; int n = 0, neg = 0; unsigned int v; char* p, *q; char c; if (radix == 10 && value < 0) { value = -value; neg = 1; } v = value; do { str[n++] = dig[v%radix]; v /= radix; } while (v); if (neg) str[n++] = '-'; str[n] = '\0'; for (p = str, q = p + (n-1); p < q; ++p, --q) c = *p, *p = *q, *q = c; return str; }
![]() |
Other Threads in the C Forum
- Previous Thread: HELP!!!!
- Next Thread: Reading Lines from Text File
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h






