函数原型
errno_t _itoa_s(
int value,
char *buffer,
size_t sizeInCharacters,
int radix
);
errno_t _i64toa_s(
__int64 value,
char *buffer,
size_t sizeInCharacters,
int radix
);
errno_t _ui64toa_s(
unsigned _int64 value,
char *buffer,
size_t sizeInCharacters,
int radix
);
errno_t _itow_s(
int value,
wchar_t *buffer,
size_t sizeInCharacters,
int radix
);
errno_t _i64tow_s(
__int64 value,
wchar_t *buffer,
size_t sizeInCharacters,
int radix
);
errno_t _ui64tow_s(
unsigned __int64 value,
wchar_t *buffer,
size_t sizeInCharacters,
int radix
);
template <size_t size>
errno_t _itoa_s(
int value,
char (&buffer)[size],
int radix
); // C++ only
template <size_t size>
errno_t _itow_s(
int value,
wchar_t (&buffer)[size],
int radix
); // C++ only
#include <string.h>
【参数】
[in] value
将转换的数字。
[out] buffer
用转换的结果进行填充。
[in] sizeInCharacters
缓冲区的大小在单字节字符或宽字符的。
[in] radix
value基;哪些必须在范围 2-36。
【返回值】
零,如果成功;在失败的错误代码。 如果满足以下任何一个应用,该函数调用无效参数处理程序,如参数验证所述。
value | 缓冲区 | sizeInCharacters | 基数 | Return |
---|---|---|---|---|
任何 | NULL | 任何 | 任何 | EINVAL |
任何 | 任何 | AMP_LT=0 | 任何 | EINVAL |
任何 | 任何 | 需要的结果字符串的长度 AMP_LT= | 任何 | EINVAL |
任何 | 任何 | 任何 | radix AMP_LT 2 或 radix AMP_GT 36 | EINVAL |
除参数和返回值, _itoa_s 函数的行为和对应的较不安全版本。
在 C++ 中,使用这些功能由模板重载简化;重载可推断缓冲区长度 (自动不再需要指定范围参数),并且还可以用以较新,安全重复自动替换旧,不安全的功能。有关更多信息,请参见 安全模板重载。
这些函数的 " debug " 版本用 0xFD 首先加载缓冲区。 若要禁用此行为,请使用 _CrtSetDebugFillThreshold。
Tchar.h 实例 | 未定义的 _UNICODE 和 _MBCS | 定义的 _MBCS | 定义的 _UNICODE |
---|---|---|---|
_itot_s | _itoa_s | _itoa_s | _itow_s |
_i64tot_s | _i64toa_s | _i64toa_s | _i64tow_s |
_ui64tot_s | _ui64toa_s | _ui64toa_s | _ui64tow_s |
【要求】
实例 | 必需的头 |
---|---|
_itoa_s | stdlib.h |
_i64toa_s | stdlib.h |
_ui64toa_s | stdlib.h |
_itow_s | stdlib.h 或 wchar.h |
_i64tow_s | stdlib.h 或 wchar.h |
_ui64tow_s | stdlib.h 或 wchar.h |
#include <stdlib.h>
#include<stdio.h>
void transfer_1()
{
char buffer[65];
int r;
for (r = 10; r >= 2; --r)
{
_itoa_s(-1, buffer, 65, r); // C4996
// Note: _itoa is deprecated; consider using _itoa_s instead
printf("base %d: %s (%d chars)\n", r, buffer, strnlen(buffer, _countof(buffer)));
}
}
void transfer_2()
{
int num = 100;
char str[100]; //空间应该足够大
int cnt = 1, tmp = num;
while (tmp /= 10)
cnt++;
sprintf_s(str, _countof(str), "%d", num);
printf("strlen(str) = %d\n", strlen(str));
printf("sizeof(str) = %d\n", sizeof(str));
printf("str = %s\n", str);
}
int main(void)
{
//方法一:
transfer_1();
printf("\n");
//方法二:
transfer_2();
printf("\n");
system("pause");
return 0;
}