Share via


_itoa_s, _ltoa_s, _ultoa_s, _i64toa_s, _ui64toa_s, _itow_s, _ltow_s, _ultow_s, _i64tow_s, _ui64tow_s

將整數轉換成字串。 這些函式是 的版本 _itoa_itow 具有 CRT 中安全性功能中所述 的安全性增強功能。

語法

errno_t _itoa_s( int value, char * buffer, size_t size, int radix );
errno_t _ltoa_s( long value, char * buffer, size_t size, int radix );
errno_t _ultoa_s( unsigned long value, char * buffer, size_t size, int radix );
errno_t _i64toa_s( long long value, char *buffer,
   size_t size, int radix );
errno_t _ui64toa_s( unsigned long long value, char *buffer,
   size_t size, int radix );

errno_t _itow_s( int value, wchar_t *buffer,
   size_t size, int radix );
errno_t _ltow_s( long value, wchar_t *buffer,
   size_t size, int radix );
errno_t _ultow_s( unsigned long value, wchar_t *buffer,
   size_t size, int radix );
errno_t _i64tow_s( long long value, wchar_t *buffer,
   size_t size, int radix );
errno_t _ui64tow_s( unsigned long long value, wchar_t *buffer,
   size_t size, int radix
);

// These template functions are C++ only:
template <size_t size>
errno_t _itoa_s( int value, char (&buffer)[size], int radix );

template <size_t size>
errno_t _ltoa_s( long value, char (&buffer)[size], int radix );

template <size_t size>
errno_t _ultoa_s( unsigned long value, char (&buffer)[size], int radix );

template <size_t size>
errno_t _itow_s( int value, wchar_t (&buffer)[size], int radix );

template <size_t size>
errno_t _ltow_s( long value, wchar_t (&buffer)[size], int radix );

template <size_t size>
errno_t _ultow_s( unsigned long value, wchar_t (&buffer)[size], int radix );

參數

value
要轉換的數字。

buffer
保留轉換結果的輸出緩衝區。

size
buffer以字元或寬字元為單位的大小。

radix
要用來轉換 value 的基數或數值基底,其必須介於 2-36 的範圍內。

傳回值

如果成功,就是零,如果失敗,則為錯誤碼。 如果適用下列任一條件,函式會叫用不正確參數處理常式,如參數驗證 中所述

錯誤條件

value 緩衝區 size 基數 傳回
任意 NULL 任意 任意 EINVAL
任意 任意 <=0 任意 EINVAL
任意 任意 <= 所需的結果字串長度 任意 EINVAL
任意 任意 任意 radix<2 或 radix> 36 EINVAL

安全性問題

如果 buffer 不是指向有效記憶體且不是 NULL ,或緩衝區的長度不夠長,則這些函式可能會產生存取違規,以保存結果字串。

備註

除了參數和傳回值之外, _itoa_s_itow_s 函式系列的行為與相對較不安全 _itoa_itow 版本相同。

C++ 利用多載樣板簡化了這些函式的使用方式。多載可自動推斷緩衝區長度 (因而不須指定大小引數),也可以將不安全的舊函式自動取代成較新且安全的對應函式。 如需詳細資訊,請參閱 保護範本多載

這些函式的偵錯程式庫版本會先將緩衝區填入0xFE。 若要停用此行為,請使用 _CrtSetDebugFillThreshold

CRT 包含方便的宏,可針對數個常見基底定義轉換每個整數類型之最長可能值的緩衝區大小,包括 Null 結束字元和符號字元。 如需詳細資訊,請參閱 最大轉換計數宏

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

泛型文字常式對應

Tchar.h 常規 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_itot_s _itoa_s _itoa_s _itow_s
_ltot_s _ltoa_s _ltoa_s _ltow_s
_ultot_s _ultoa_s _ultoa_s _ultow_s
_i64tot_s _i64toa_s _i64toa_s _i64tow_s
_ui64tot_s _ui64toa_s _ui64toa_s _ui64tow_s

需求

常式 必要的標頭
_itoa_s, _ltoa_s, _ultoa_s, _i64toa_s, _ui64toa_s <stdlib.h>
_itow_s, _ltow_s, _ultow_s, _i64tow_s, _ui64tow_s <stdlib.h><wchar.h>

這些函式是 Microsoft 特有的。 如需相容性詳細資訊,請參閱相容性

範例

此範例示範使用一些整數轉換函式。 宏 _countof 只有在編譯器可以看到陣列宣告時,才能判斷緩衝區大小,而不適用於已衰落至指標的參數。

// crt_itoa_s.c
// Compile by using: cl /W4 crt_itoa_s.c
#include <stdlib.h>     // for _itoa_s functions, _countof, count macro
#include <stdio.h>      // for printf
#include <string.h>     // for strnlen

int main( void )
{
    char buffer[_MAX_U64TOSTR_BASE2_COUNT];
    int r;
    for ( r = 10; r >= 2; --r )
    {
        _itoa_s( -1, buffer, _countof(buffer), r );
        printf( "base %d: %s (%d chars)\n",
            r, buffer, strnlen(buffer, _countof(buffer)) );
    }
    printf( "\n" );
    for ( r = 10; r >= 2; --r )
    {
        _i64toa_s( -1LL, buffer, _countof(buffer), r );
        printf( "base %d: %s (%d chars)\n",
            r, buffer, strnlen(buffer, _countof(buffer)) );
    }
    printf( "\n" );
    for ( r = 10; r >= 2; --r )
    {
        _ui64toa_s( 0xffffffffffffffffULL, buffer, _countof(buffer), r );
        printf( "base %d: %s (%d chars)\n",
            r, buffer, strnlen(buffer, _countof(buffer)) );
    }
}
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)

base 10: -1 (2 chars)
base 9: 145808576354216723756 (21 chars)
base 8: 1777777777777777777777 (22 chars)
base 7: 45012021522523134134601 (23 chars)
base 6: 3520522010102100444244423 (25 chars)
base 5: 2214220303114400424121122430 (28 chars)
base 4: 33333333333333333333333333333333 (32 chars)
base 3: 11112220022122120101211020120210210211220 (41 chars)
base 2: 1111111111111111111111111111111111111111111111111111111111111111 (64 chars)

base 10: 18446744073709551615 (20 chars)
base 9: 145808576354216723756 (21 chars)
base 8: 1777777777777777777777 (22 chars)
base 7: 45012021522523134134601 (23 chars)
base 6: 3520522010102100444244423 (25 chars)
base 5: 2214220303114400424121122430 (28 chars)
base 4: 33333333333333333333333333333333 (32 chars)
base 3: 11112220022122120101211020120210210211220 (41 chars)
base 2: 1111111111111111111111111111111111111111111111111111111111111111 (64 chars)

另請參閱

資料轉換
_itoa_itow 函式