Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How can I convert numbers to strings (the opposite of atoi)? Is there an itoa function?
How can I convert numbers to strings (the opposite of atoi)? Is there an itoa function?
✍: Guest
Just use sprintf:
sprintf(string, "%d", number);
(Don't worry that sprintf may be overkill, potentially wasting run time or code space; it works well in practice.)
You can obviously use sprintf to convert long or floating-point numbers to strings as well (using %ld or %f); in other words, sprintf can also be thought of as the opposite of atol and atof. In addition, you have quite a bit of control over the formatting. (It's for these reasons that C supplies sprintf as a general solution, and not itoa.)
If you simply must write an itoa function, here are some things to consider:
* There is a sample implementation in K&R.
* You'll have to worry about return buffer allocation;
# A naïve implementation usually doesn't handle the most-negative integer (INT_MIN, usually -32,768 or -2,147,483,648) properly.
2015-08-24, 2591👍, 0💬
Popular Posts:
How does ASP.NET maintain state in between subsequent request ? Refer caching chapter.
Which one of the following statements is TRUE in regard to overloading the ++ operator? 1 You cannot...
How To List All Values of Submitted Fields? - PHP Script Tips - Processing Web Forms If you want lis...
Write an equivalent expression for x%8? x&7
How To Control Table Widths? - XHTML 1.0 Tutorials - Understanding Tables and Table Cells Usually, b...