_itoa ()- 将整数转换为字符串

格式

#include <stdlib.h>
char *_itoa(int value, char *string, int radix);
注: _itoa 函数仅支持 C + + ,而不支持 C。

语言级别

分机

线程安全

描述

_itoa() 将给定 的数字转换为以空字符结尾的字符串,并将结果存储在 string中。 Radix 参数指定 value的基数; 它必须在 2 到 36 的范围内。 如果 基数 等于 10 且 为负数,那么存储的字符串的第一个字符为减号 (-)。

注:string 保留的空间必须足以容纳返回的字符串。 该函数最多可以返回 33 个字节,包括空字符 (\0)。

返回值

_itoa 返回指向 string的指针。 没有错误返回值。

当字符串自变量为 NULL基数 超出范围 2 到 36 时, errno 将设置为 EINVAL。

示例

此示例将整数值 -255 转换为十进制,二进制和十六进制数字,并将其字符表示存储在数组 缓冲区中。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
   char buffer[35];
   char *p;
   p = _itoa(-255, buffer, 10);
   printf("The result of _itoa(-255) with radix of 10 is %s\n", p);
   p = _itoa(-255, buffer, 2);
   printf("The result of _itoa(-255) with radix of 2\n    is %s\n", p);
   p = _itoa(-255, buffer, 16);
   printf("The result of _itoa(-255) with radix of 16 is %s\n", p);
   return 0;
}
输出应该为:
      The result of _itoa(-255) with radix of 10 is -255
      The result of _itoa(-255) with radix of 2
          is 11111111111111111111111100000001
      The result of _itoa(-255) with radix of 16 is ffffff01

相关信息