• itoa 和_itoa_s


    1 itoa, 将整数转换为字符串。

    char *  itoa ( int value, char * buffer, int radix );

    它包含三个参数:

    value, 是要转换的数字;

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

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


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

    Example:

    #include <stdio.h>
    #include <stdlib.h>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int n;
        char buffer[33];
        printf("Enter a number:");
        scanf("%d",&n);
        itoa(n,buffer,10);
        printf("decimal: %s
    ", buffer);
    
        itoa(n,buffer,16);
        printf("hexadecimal: %s
    ", buffer);
    
        itoa(n,buffer,2);
        printf("binary: %s
    ",buffer);
        
    return 0;
    }
    View Code

    Output:

    Enter a number:6666
    decimal: 6666
    hexadecimal: 1a0a
    binary: 1101000001010
    Output

    有关 itoa 的详细介绍,请参照 itoa : Convert integer to string.

    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 <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    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)
    ", r, buffer, strnlen(buffer, _countof(buffer)) );
        }
        
        return 0;
    }
    View Code

    Output:

    base 10: -1 (2 chars)
    base 9: 12068657453 (11 chars)
    base 8: 37777777777 (11 chars)
    base 7: 211301422353 (12 chars)
    base 6: 1550104015503 (13 chars)
    base 5: 32244002423140 (14 chars)
    base 4: 3333333333333333 (16 chars)
    base 3: 102002022201221111210 (21 chars)
    base 2: 11111111111111111111111111111111 (32 chars)
    Output

    有关 _itoa_s 的详细介绍,请参照_itoa_s, _i64toa_s, _ui64toa_s, _itow_s, _i64tow_s, _ui64tow_s

  • 相关阅读:
    7.12Java+TestNG环境搭建
    7.15Java之调用API接口传表单获取返回信息
    7.12理解Cookie与token
    7.13一次完整的Http请求过程(3)
    7.13一次完整的Http请求过程(2)
    7.13一次完整的Http请求过程
    PostgreSql安装(win 2003 下)
    实用工具(渐变更新中)
    .net Ajax系列(2)调用多Web Service
    .net Ajax系列(1)调用Web Service
  • 原文地址:https://www.cnblogs.com/cindy-hu-23/p/3548211.html
Copyright © 2020-2023  润新知