Skip to content

Latest commit

 

History

History
243 lines (186 loc) · 13.9 KB

itoa-itow.md

File metadata and controls

243 lines (186 loc) · 13.9 KB
titledescriptionms.dateapi_nameapi_locationapi_typetopic_typef1_keywordshelpviewer_keywords
_itoa, _itow functions
API reference for _itoa, and _itow; which convert an integer to a string.
4/2/2020
itoa
_itoa
ltoa
_ltoa
ultoa
_ultoa
_i64toa
_ui64toa
_itow
_ltow
_ultow
_i64tow
_ui64tow
_o__i64toa
_o__i64tow
_o__itoa
_o__itow
_o__ltoa
_o__ltow
_o__ui64toa
_o__ui64tow
_o__ultoa
_o__ultow
msvcrt.dll
msvcr80.dll
msvcr90.dll
msvcr100.dll
msvcr100_clr0400.dll
msvcr110.dll
msvcr110_clr0400.dll
msvcr120.dll
msvcr120_clr0400.dll
ucrtbase.dll
api-ms-win-crt-convert-l1-1-0.dll
ntoskrnl.exe
DLLExport
apiref
_itoa
_ltoa
_ultoa
_i64toa
_ui64toa
_itow
_ltow
_ultow
_i64tow
_ui64tow
itoa
ltoa
ultoa
i64toa
ui64toa
itow
ltow
ultow
i64tow
ui64tow
itot
_itot
ltot
_ltot
ultot
_ultot
i64tot
_i64tot
ui64tot
_ui64tot
_MAX_ITOSTR_BASE16_COUNT
_MAX_ITOSTR_BASE10_COUNT
_MAX_ITOSTR_BASE8_COUNT
_MAX_ITOSTR_BASE2_COUNT
_MAX_LTOSTR_BASE16_COUNT
_MAX_LTOSTR_BASE10_COUNT
_MAX_LTOSTR_BASE8_COUNT
_MAX_LTOSTR_BASE2_COUNT
_MAX_ULTOSTR_BASE16_COUNT
_MAX_ULTOSTR_BASE10_COUNT
_MAX_ULTOSTR_BASE8_COUNT
_MAX_ULTOSTR_BASE2_COUNT
_MAX_I64TOSTR_BASE16_COUNT
_MAX_I64TOSTR_BASE10_COUNT
_MAX_I64TOSTR_BASE8_COUNT
_MAX_I64TOSTR_BASE2_COUNT
_MAX_U64TOSTR_BASE16_COUNT
_MAX_U64TOSTR_BASE10_COUNT
_MAX_U64TOSTR_BASE8_COUNT
_MAX_U64TOSTR_BASE2_COUNT
_itot function
ui64toa function
_ui64toa function
converting integers
itot function
_i64tow function
_i64toa function
_itow function
ui64tow function
integers, converting
itoa function
_ui64tow function
i64tow function
itow function
i64toa function
converting numbers, to strings
_itoa function

itoa, _itoa, ltoa, _ltoa, ultoa, _ultoa, _i64toa, _ui64toa, _itow, _ltow, _ultow, _i64tow, _ui64tow

Converts an integer to a string. More secure versions of these functions are available, see _itoa_s, _itow_s functions.

Syntax

char*_itoa( intvalue, char*buffer, intradix ); char*_ltoa( longvalue, char*buffer, intradix ); char*_ultoa( unsigned longvalue, char*buffer, intradix ); char*_i64toa( long longvalue, char*buffer, intradix ); char*_ui64toa( unsigned long longvalue, char*buffer, intradix ); wchar_t*_itow( intvalue, wchar_t*buffer, intradix ); wchar_t*_ltow( longvalue, wchar_t*buffer, intradix ); wchar_t*_ultow( unsigned longvalue, wchar_t*buffer, intradix ); wchar_t*_i64tow( long longvalue, wchar_t*buffer, intradix ); wchar_t*_ui64tow( unsigned long longvalue, wchar_t*buffer, intradix ); // These POSIX versions of the functions have deprecated names:char*itoa( intvalue, char*buffer, intradix ); char*ltoa( longvalue, char*buffer, intradix ); char*ultoa( unsigned longvalue, char*buffer, intradix ); // The following template functions are C++ only:template<size_tsize>char*_itoa( intvalue, char (&buffer)[size], intradix ); template<size_tsize>char*_itoa( longvalue, char (&buffer)[size], intradix ); template<size_tsize>char*_itoa( unsignedlongvalue, char (&buffer)[size], intradix ); template<size_tsize>char*_i64toa( longlongvalue, char (&buffer)[size], intradix ); template<size_tsize>char*_ui64toa( unsignedlong long value, char (&buffer)[size], intradix ); template<size_tsize>wchar_t*_itow( intvalue, wchar_t (&buffer)[size], intradix ); template<size_tsize>wchar_t*_ltow( longvalue, wchar_t (&buffer)[size], intradix ); template<size_tsize>wchar_t*_ultow( unsignedlongvalue, wchar_t (&buffer)[size], intradix ); template<size_tsize>wchar_t*_i64tow( longlongvalue, wchar_t (&buffer)[size], intradix ); template<size_tsize>wchar_t*_ui64tow( unsignedlong long value, wchar_t (&buffer)[size], intradix );

Parameters

value
Number to be converted.

buffer
Buffer that holds the result of the conversion.

radix
The base to use for the conversion of value, which must be in the range 2-36.

size
Length of the buffer in units of the character type. This parameter is inferred from the buffer argument in C++.

Return value

Each of these functions returns a pointer to buffer. There's no error return.

Remarks

The _itoa, _ltoa, _ultoa, _i64toa, and _ui64toa functions convert the digits of the given value argument to a null-terminated character string and store the result (up to 33 characters for _itoa, _ltoa, and _ultoa, and 65 for _i64toa and _ui64toa) in buffer. If radix equals 10 and value is negative, the first character of the stored string is the minus sign (-). The _itow, _ltow, _ultow, _i64tow, and _ui64tow functions are wide-character versions of _itoa, _ltoa, _ultoa, _i64toa, and _ui64toa, respectively.

Important

These functions can write past the end of a buffer that is too small. To prevent buffer overruns, ensure that buffer is large enough to hold the converted digits plus the trailing null-character and a sign character. Misuse of these functions can cause serious security issues in your code.

Because of their potential for security issues, by default, these functions cause deprecation warning C4996: This function or variable may be unsafe. Consider using safe_function instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. We recommend you change your source code to use the safe_function suggested by the warning message. The more secure functions don't write more characters than the specified buffer size. For more information, see _itoa_s, _itow_s functions.

To use these functions without the deprecation warning, define the _CRT_SECURE_NO_WARNINGS preprocessor macro before including any CRT headers. You can define it by adding the /D_CRT_SECURE_NO_WARNINGS compiler option to the cl command. Otherwise, define the macro in your source files. If you use precompiled headers, define the macro at the top of the precompiled header include file, pch.h (stdafx.h in Visual Studio 2017 and earlier). To define the macro in your source code, use a #define directive before you include any CRT header, as in this example:

#define_CRT_SECURE_NO_WARNINGS 1 #include<stdlib.h>

In C++, these functions have template overloads that invoke their safer counterparts. For more information, see Secure template overloads.

By default, these functions' global state is scoped to the application. To change this behavior, see Global state in the CRT.

The POSIX names itoa, ltoa, and ultoa exist as aliases for the _itoa, _ltoa, and _ultoa functions. The POSIX names are deprecated because they don't follow the implementation-specific global function name conventions of ISO C. By default, these functions cause deprecation warning C4996: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name:new_name. We recommend you change your source code to use the safer versions of these functions, _itoa_s, _ltoa_s, or _ultoa_s. For more information, see _itoa_s, _itow_s functions.

For source code portability, you may prefer to retain the POSIX names in your code. To use these functions without the deprecation warning, define both the _CRT_NONSTDC_NO_WARNINGS and _CRT_SECURE_NO_WARNINGS preprocessor macros before including any CRT headers. You can define them by adding the /D_CRT_SECURE_NO_WARNINGS and /D_CRT_NONSTDC_NO_WARNINGS compiler options to the cl command. Otherwise, define the macros in your source files. If you use precompiled headers, define the macros at the top of the precompiled header include file. To define the macros in your source code, use #define directives before you include any CRT header, as in this example:

#define_CRT_NONSTDC_NO_WARNINGS 1 #define_CRT_SECURE_NO_WARNINGS 1 #include<stdlib.h>

Maximum conversion count macros

To help you create secure buffers for conversions, the CRT includes some convenient macros. These macros define the size of the buffer required to convert the longest possible value of each integer type, including the null terminator and sign character, for several common bases. To ensure that your conversion buffer is large enough to receive any conversion in the base specified by radix, use one of these defined macros when you allocate the buffer. The macros help you prevent buffer overrun errors when you convert integral types to strings. These macros are defined when you include either stdlib.h or wchar.h in your source.

To use one of these macros in a string conversion function, declare your conversion buffer of the appropriate character type and use the macro value for the integer type and base as the buffer dimension. This table lists the macros that are appropriate for each function for the listed bases:

FunctionsradixMacros
_itoa, _itow16
10
8
2
_MAX_ITOSTR_BASE16_COUNT
_MAX_ITOSTR_BASE10_COUNT
_MAX_ITOSTR_BASE8_COUNT
_MAX_ITOSTR_BASE2_COUNT
_ltoa, _ltow16
10
8
2
_MAX_LTOSTR_BASE16_COUNT
_MAX_LTOSTR_BASE10_COUNT
_MAX_LTOSTR_BASE8_COUNT
_MAX_LTOSTR_BASE2_COUNT
_ultoa, _ultow16
10
8
2
_MAX_ULTOSTR_BASE16_COUNT
_MAX_ULTOSTR_BASE10_COUNT
_MAX_ULTOSTR_BASE8_COUNT
_MAX_ULTOSTR_BASE2_COUNT
_i64toa, _i64tow16
10
8
2
_MAX_I64TOSTR_BASE16_COUNT
_MAX_I64TOSTR_BASE10_COUNT
_MAX_I64TOSTR_BASE8_COUNT
_MAX_I64TOSTR_BASE2_COUNT
_ui64toa, _ui64tow16
10
8
2
_MAX_U64TOSTR_BASE16_COUNT
_MAX_U64TOSTR_BASE10_COUNT
_MAX_U64TOSTR_BASE8_COUNT
_MAX_U64TOSTR_BASE2_COUNT

This example uses a conversion count macro to define a buffer large enough to contain an unsigned long long in base 2:

#include<wchar.h> #include<iostream>intmain() { wchar_t buffer[_MAX_U64TOSTR_BASE2_COUNT]; std::wcout << _ui64tow(0xFFFFFFFFFFFFFFFFull, buffer, 2) << std::endl; }

Generic-text routine mappings

Tchar.h routine_UNICODE and _MBCS not defined_MBCS defined_UNICODE defined
_itot_itoa_itoa_itow
_ltot_ltoa_ltoa_ltow
_ultot_ultoa_ultoa_ultow
_i64tot_i64toa_i64toa_i64tow
_ui64tot_ui64toa_ui64toa_ui64tow

Requirements

RoutineRequired header
itoa, ltoa, ultoa<stdlib.h>
_itoa, _ltoa, _ultoa, _i64toa, _ui64toa<stdlib.h>
_itow, _ltow, _ultow, _i64tow, _ui64tow<stdlib.h> or <wchar.h>

These functions and macros are Microsoft-specific. For more compatibility information, see Compatibility.

Example

This sample demonstrates the use of some of the integer conversion functions. Note the use of the _CRT_SECURE_NO_WARNINGS macro to silence warning C4996.

// crt_itoa.c// Compile by using: cl /W4 crt_itoa.c// This program makes use of the _itoa functions// in various examples.#define_CRT_SECURE_NO_WARNINGS 1 #include<stdio.h>// for printf#include<string.h>// for strnlen#include<stdlib.h>// for _countof, _itoa fns, _MAX_COUNT macrosintmain(void) { charbuffer[_MAX_U64TOSTR_BASE2_COUNT]; intr; for (r=10; r >= 2; --r) { _itoa(-1, buffer, r); printf("base %d: %s (%d chars)\n", r, buffer, strnlen(buffer, _countof(buffer))); } printf("\n"); for (r=10; r >= 2; --r) { _i64toa(-1LL, buffer, r); printf("base %d: %s (%d chars)\n", r, buffer, strnlen(buffer, _countof(buffer))); } printf("\n"); for (r=10; r >= 2; --r) { _ui64toa(0xffffffffffffffffULL, 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) 

See also

Data conversion
_itoa_s, _itow_s functions

close