itoa atoi sprintf

来源:互联网 发布:centos 不显示中文 编辑:程序博客网 时间:2024/05/18 01:30


// atoi: 将一个 字符串转换为 int 类型const char ch1[32] = "123";int getCh1 = atoi(ch1);cout<<"getCh1 = "<<getCh1<<endl;


itoa()函数有3个参数:
第一个参数是要转换的数字,
第二个参数是要写入转换结果的目标字符串,
第三个参数是转移数字时所用 的基数。在上例中,转换基数为10。10:十进制;2:二进制...

itoa并不是一个标准的 C 函数,它是 Windows 特有的,如果要写跨平台的程序,请用 sprintf。
是Windows平台下扩展的,标准库中有 sprintf,功能比这个更强,用法跟 printf 类似:

// itoa : 将一个 int 类型的值转换成字符串char ch2[32];_itoa_s(321, ch2, 10); <span style="white-space:pre"></span>// 同 itoa cout<<"ch2 = "<<ch2<<endl;


char str[255];sprintf(str, "%x", 100); //将100转为16进制表示的字符串。 正常情况用 %dcout<<"str = "<<str<<endl;




0 0
原创粉丝点击