与 atoi() 相反的功能?

function opposite to atoi()?

我想将整数转换为字符串,例如:

int a = 12345

隐藏到

char b[6] = "12345"

基本上与将字符串转换为整数的 atoi 函数相反。

使用 itoa()。这应该可以解决问题。(只有一些编译器有)

#include <stdio.h>
int sprintf(char *str, const char *format, ...);

示例

char str[10]; 
sprintf(str,"%d", 12345);

来源:Here

char * itoa ( int value, char * str, int base )

Convert integer to string (non-standard function) Converts an integer value to a null-terminated string using the specified base and stores the result in the array given by str parameter.

If base is 10 and value is negative, the resulting string is preceded with a minus sign (-). With any other base, value is always considered unsigned.

str should be an array long enough to contain any possible value: (sizeof(int)*8+1) for radix=2, i.e. 17 bytes in 16-bits platforms and 33 in 32-bits platforms.

但未在 ANSI-C 中定义,也不是 C++ 的一部分

某些情况下符合标准的替代方法可能是 sprintf:

  • sprintf(str,"%d",value) 转换为十进制。