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)
?
相关问题
- Multiple sockets for clients to connect to
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- how to get running process information in java?
You can use this program instead of sprintf.
glibc internal implementation
glibc 2.28 has an internal implementation:
which is used in several places internally, but I could not find if it can be exposed or how.
At least that should be a robust implementation if you are willing to extract it.
This question asks how to roll your own: How to convert an int to string in C
As Matt J wrote, there is
itoa
, but it's not standard. Your code will be more portable if you usesnprintf
.Here is a much improved version of Archana's solution. It works for any radix 1-16, and numbers <= 0, and it shouldn't clobber memory.