itoa 和_itoa_s

itoa 和_itoa_s

1.itoa (将整数转换为字符串)
char * itoa ( int value, char * buffer, int radix );

它包含三个参数:

value, 是要转换的数字;

buffer, 是存放转换结果的字符串;

radix, 是转换所用的基数,2-36。如,2:二进制,10:十进制,16:十六进制

扩展:
ltoa() 将长整型值转换为字符串
ultoa() 将无符号长整型值转换为字符串

例子,将一个十进制数转化成二进制表示的字符串

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long  
using namespace std;
char s[32];
int main()
{
   int n;
   while(cin>>n){
    itoa(n,s,2);   
    cout<return 0;
}

2._itoa_s (是 itoa 的安全版本,除了参数和返回值不同,两个函数的行为是相同的,都是将整数转换为字符串)

errno_t _itoa_s(int value, char *buffer, size_t sizeInCharacters, int radix);

_itoa_s 比 itoa 多出一个参数:

value, 是要转换的数字;

buffer, 是存放转换结果的字符串;

sizeInCharacters, 存放转换结果的字符串长度

radix, 是转换所用的基数,2-36。如,2:二进制,10:十进制,16:十六进制、

Example

#include 
#include 
#include 


int _tmain(int argc, _TCHAR* argv[])
{
    char buffer[65];
    int r;
    for( r=10; r>=2; --r )
    {
       _itoa_s( -1, buffer, 65, r );
       printf( "base %d: %s (%d chars)\n", r, buffer, strnlen(buffer, _countof(buffer)) );
    }

    return 0;
}

头文件:c中是 stdlib.h,c++中是cstdlib

你可能感兴趣的:(itoa,和_itoa_s)