Parsing Integer to String C

2019-01-22 13:10发布

How does one parse an integer to string(char* || char[]) in C? Is there an equivalent to the Integer.parseInt(String) method from Java in C?

9条回答
时光不老,我们不散
2楼-- · 2019-01-22 13:43

This is discussed in Steve Summit's C FAQs.

查看更多
唯我独甜
3楼-- · 2019-01-22 13:45

You can try:

int intval;
String stringval;
//assign a value to intval here.
stringval = String(intval);

that should do the trick.

查看更多
来,给爷笑一个
4楼-- · 2019-01-22 13:47

If you want to convert an integer to string, try the function snprintf().

If you want to convert a string to an integer, try the function sscanf() or atoi() or atol().

查看更多
冷血范
5楼-- · 2019-01-22 13:48

To convert an int to a string:

int x = -5;
char buffer[50];
sprintf( buffer, "%d", x );

You can also do it for doubles:

double d = 3.1415;
sprintf( buffer, "%f", d );

To convert a string to an int:

int x = atoi("-43");

See http://www.acm.uiuc.edu/webmonkeys/book/c_guide/ for the documentation of these functions.

查看更多
贪生不怕死
6楼-- · 2019-01-22 13:51

The Java parseInt() function parses a string to return an integer. An equivalent C function is atoi(). However, this doesn't seem to match the first part of your question. Do you want to convert from an integer to a string, or from a string to an integer?

查看更多
疯言疯语
7楼-- · 2019-01-22 13:53

You may want to take a look at the compliant solution on this site.

查看更多
登录 后发表回答