Skip to content

Commit 8918789

Browse files
author
Colin Robertson
committed
Updates to format, macros, sample code
1 parent 756408e commit 8918789

File tree

1 file changed

+76
-131
lines changed

1 file changed

+76
-131
lines changed

docs/c-runtime-library/reference/itoa-i64toa-ui64toa-itow-i64tow-ui64tow.md

+76-131
Original file line numberDiff line numberDiff line change
@@ -27,82 +27,49 @@ Converts an integer to a string. More secure versions of these functions are ava
2727
## Syntax
2828

2929
```C
30-
char *_itoa(
31-
int value,
32-
char *buffer,
33-
int radix
34-
);
35-
char *_ltoa(
36-
long value,
37-
char *buffer,
38-
int radix
39-
);
40-
char *_ultoa(
41-
unsigned long value,
42-
char *buffer,
43-
int radix
44-
);
45-
char *_i64toa(
46-
__int64 value,
47-
char *buffer,
48-
int radix
49-
);
50-
char * _ui64toa(
51-
unsigned _int64 value,
52-
char *buffer,
53-
int radix
54-
);
55-
wchar_t * _itow(
56-
int value,
57-
wchar_t *buffer,
58-
int radix
59-
);
60-
wchar_t * _i64tow(
61-
__int64 value,
62-
wchar_t *buffer,
63-
int radix
64-
);
65-
wchar_t * _ui64tow(
66-
unsigned __int64 value,
67-
wchar_t *buffer,
68-
int radix
69-
);
30+
char * _itoa( int value, char *buffer, int radix );
31+
char * _ltoa( long value, char *buffer, int radix );
32+
char * _ultoa( unsigned long value, char *buffer, int radix );
33+
char * _i64toa( long long value, char *buffer, int radix );
34+
char * _ui64toa( unsigned long long value, char *buffer, int radix );
35+
36+
wchar_t * _itow( int value, wchar_t *buffer, int radix );
37+
wchar_t * _ltow( long value, wchar_t *buffer, int radix );
38+
wchar_t * _ultow( unsigned long value, wchar_t *buffer, int radix );
39+
wchar_t * _i64tow( long long value, wchar_t *buffer, int radix );
40+
wchar_t * _ui64tow( unsigned long long value, wchar_t *buffer, int radix );
41+
42+
// The following template functions are C++ only:
7043
template <size_t size>
71-
char *_itoa(
72-
int value,
73-
char (&buffer)[size],
74-
int radix
75-
); // C++ only
44+
char *_itoa( int value, char (&buffer)[size], int radix );
45+
46+
template <size_t size>
47+
char *_itoa( long value, char (&buffer)[size], int radix );
48+
7649
template <size_t size>
77-
char *_i64toa(
78-
__int64 value,
79-
char (&buffer)[size],
80-
int radix
81-
); // C++ only
50+
char *_itoa( unsigned long value, char (&buffer)[size], int radix );
51+
8252
template <size_t size>
83-
char * _ui64toa(
84-
unsigned _int64 value,
85-
char (&buffer)[size],
86-
int radix
87-
); // C++ only
53+
char *_i64toa( long long value, char (&buffer)[size], int radix );
54+
8855
template <size_t size>
89-
wchar_t * _itow(
90-
int value,
91-
wchar_t (&buffer)[size],
92-
int radix
93-
); // C++ only
56+
char * _ui64toa( unsigned long long value, char (&buffer)[size], int radix );
57+
9458
template <size_t size>
95-
wchar_t * _i64tow(
96-
__int64 value,
97-
wchar_t (&buffer)[size],
98-
int radix
99-
); // C++ only
59+
wchar_t * _itow( int value, wchar_t (&buffer)[size], int radix );
60+
10061
template <size_t size>
101-
wchar_t * _ui64tow(
102-
unsigned __int64 value,
103-
wchar_t (&buffer)[size],
104-
int radix
105-
); // C++ only
62+
wchar_t * _ltow( long value, wchar_t (&buffer)[size], int radix );
63+
64+
template <size_t size>
65+
wchar_t * _ultow( unsigned long value, wchar_t (&buffer)[size], int radix );
66+
67+
template <size_t size>
68+
wchar_t * _i64tow( long long value, wchar_t (&buffer)[size], int radix );
69+
70+
template <size_t size>
71+
wchar_t * _ui64tow( unsigned long long value, wchar_t (&buffer)[size],
72+
int radix );
10673
```
10774
10875
### Parameters
@@ -114,92 +81,69 @@ Number to be converted.
11481
String result.
11582
11683
*radix*<br/>
117-
Base of *value*; which must be in the range 2-36.
84+
The base to use for the conversion of *value*, which must be in the range 2-36.
11885
11986
*size*<br/>
120-
Length of the buffer in units of the character type. This parameter is inferred in C++.
87+
Length of the buffer in units of the character type. This parameter is inferred from the *buffer* argument in C++.
12188
12289
## Return Value
12390
12491
Each of these functions returns a pointer to *buffer*. There is no error return.
12592
12693
## Remarks
12794
128-
The `_itoa`, `_i64toa`, and `_ui64toa` functions convert the digits of the given *value* argument to a null-terminated character string and stores the result (up to 33 characters for `_itoa` 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 ( `-` ). `_itow`, `_i64tow`, and `_ui64tow` are wide-character versions of `_itoa`, `_i64toa`, and `_ui64toa`, respectively.
129-
130-
> [!IMPORTANT]
131-
> To prevent buffer overruns, ensure that the *buffer* buffer is large enough to hold the converted digits plus the trailing null-character and a sign character. For convenience, stdlib.h and wchar.h define macros that specify the required count of narrow or wide characters required for each type of conversion. For more information, see [Maximum conversion count macros](#maximum-conversion-count-macros).
95+
The `_itoa`, `_ltoa`, `_ultoa`, `_i64toa`, and `_ui64toa` functions convert the digits of the given *value* argument to a null-terminated character string and stores 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.
13296
13397
In C++, these functions have template overloads that invoke the newer, secure counterparts of these functions. For more information, see [Secure Template Overloads](../../c-runtime-library/secure-template-overloads.md).
13498
99+
> [!IMPORTANT]
100+
> To prevent buffer overruns, ensure that *buffer* is large enough to hold the converted digits plus the trailing null-character and a sign character.
101+
135102
### Maximum conversion count macros
136103
137-
The Microsoft implementation of the CRT includes some convenient macros for the size of the buffer required to convert the longest possible integer of each type. 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. This helps to prevent buffer overrun errors when you convert integral types to strings.
104+
To help you create secure buffers for conversions, the Microsoft implementation of the CRT includes some convenient macros for the size of the buffer required to convert the longest possible integer of each 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. This helps to 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.
138105
139-
These macros are defined when you include either stdlib.h or wchar.h in your source:
140-
141-
```C
142-
// Maximum number of elements, including null terminator (and negative sign
143-
// where appropriate), needed for integer-to-string conversions for several
144-
// bases and integer types.
145-
#define _MAX_ITOSTR_BASE16_COUNT (8 + 1)
146-
#define _MAX_ITOSTR_BASE10_COUNT (1 + 10 + 1)
147-
#define _MAX_ITOSTR_BASE8_COUNT (11 + 1)
148-
#define _MAX_ITOSTR_BASE2_COUNT (32 + 1)
149-
150-
#define _MAX_LTOSTR_BASE16_COUNT (8 + 1)
151-
#define _MAX_LTOSTR_BASE10_COUNT (1 + 10 + 1)
152-
#define _MAX_LTOSTR_BASE8_COUNT (11 + 1)
153-
#define _MAX_LTOSTR_BASE2_COUNT (32 + 1)
154-
155-
#define _MAX_ULTOSTR_BASE16_COUNT (8 + 1)
156-
#define _MAX_ULTOSTR_BASE10_COUNT (10 + 1)
157-
#define _MAX_ULTOSTR_BASE8_COUNT (11 + 1)
158-
#define _MAX_ULTOSTR_BASE2_COUNT (32 + 1)
159-
160-
#define _MAX_I64TOSTR_BASE16_COUNT (16 + 1)
161-
#define _MAX_I64TOSTR_BASE10_COUNT (1 + 19 + 1)
162-
#define _MAX_I64TOSTR_BASE8_COUNT (22 + 1)
163-
#define _MAX_I64TOSTR_BASE2_COUNT (64 + 1)
164-
165-
#define _MAX_U64TOSTR_BASE16_COUNT (16 + 1)
166-
#define _MAX_U64TOSTR_BASE10_COUNT (20 + 1)
167-
#define _MAX_U64TOSTR_BASE8_COUNT (22 + 1)
168-
#define _MAX_U64TOSTR_BASE2_COUNT (64 + 1)
169-
```
170-
171-
To use one of these macros, declare your conversion buffer of the appropriate type and use the macro value for the dimension. For example,
106+
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:
172107
173108
||||
174109
|-|-|-|
175-
|Function|radix|Macro|
176-
|`_itoa`, `_itow`|16<br/>10<br/>8<br/>2|`_MAX_ITOSTR_BASE16_COUNT`<br/>`_MAX_ITOSTR_BASE10_COUNT`<br/>`_MAX_ITOSTR_BASE8_COUNT`<br/>`_MAX_ITOSTR_BASE2_COUNT`|
177-
|`_ltoa`, `_ltow`|16<br/>10<br/>8<br/>2|`_MAX_ITOSTR_BASE16_COUNT`<br/>`_MAX_ITOSTR_BASE10_COUNT`<br/>`_MAX_ITOSTR_BASE8_COUNT`<br/>`_MAX_ITOSTR_BASE2_COUNT`|
178-
|`_itoa`, `_itow`|16<br/>10<br/>8<br/>2|`_MAX_ITOSTR_BASE16_COUNT`<br/>`_MAX_ITOSTR_BASE10_COUNT`<br/>`_MAX_ITOSTR_BASE8_COUNT`<br/>`_MAX_ITOSTR_BASE2_COUNT`|
110+
|Functions|radix|Macros|
179111
|`_itoa`, `_itow`|16<br/>10<br/>8<br/>2|`_MAX_ITOSTR_BASE16_COUNT`<br/>`_MAX_ITOSTR_BASE10_COUNT`<br/>`_MAX_ITOSTR_BASE8_COUNT`<br/>`_MAX_ITOSTR_BASE2_COUNT`|
112+
|`_ltoa`, `_ltow`|16<br/>10<br/>8<br/>2|`_MAX_LTOSTR_BASE16_COUNT`<br/>`_MAX_LTOSTR_BASE10_COUNT`<br/>`_MAX_LTOSTR_BASE8_COUNT`<br/>`_MAX_LTOSTR_BASE2_COUNT`|
113+
|`_ultoa`, `_ultow`|16<br/>10<br/>8<br/>2|`_MAX_ULTOSTR_BASE16_COUNT`<br/>`_MAX_ULTOSTR_BASE10_COUNT`<br/>`_MAX_ULTOSTR_BASE8_COUNT`<br/>`_MAX_ULTOSTR_BASE2_COUNT`|
114+
|`_i64toa`, `_i64tow`|16<br/>10<br/>8<br/>2|`_MAX_I64TOSTR_BASE16_COUNT`<br/>`_MAX_I64TOSTR_BASE10_COUNT`<br/>`_MAX_I64TOSTR_BASE8_COUNT`<br/>`_MAX_I64TOSTR_BASE2_COUNT`|
115+
|`_ui64toa`, `_ui64tow`|16<br/>10<br/>8<br/>2|`_MAX_U64TOSTR_BASE16_COUNT`<br/>`_MAX_U64TOSTR_BASE10_COUNT`<br/>`_MAX_U64TOSTR_BASE8_COUNT`<br/>`_MAX_U64TOSTR_BASE2_COUNT`|
180116
117+
This example uses a conversion count macro to define a buffer large enough to contain an unsigned long in base 2:
181118
119+
```cpp
120+
#include <wchar.h>
121+
#include <iostream>
122+
int main()
123+
{
124+
wchar_t buffer[_MAX_ULTOSTR_BASE2_COUNT];
125+
std:wcout << _ultow(3000000000ul, buffer, 2) << std::endl;
126+
}
127+
```
182128

183129
### Generic-Text Routine Mappings
184130

185131
|Tchar.h routine|_UNICODE and _MBCS not defined|_MBCS defined|_UNICODE defined|
186132
|---------------------|--------------------------------------|--------------------|-----------------------|
187133
|`_itot`|`_itoa`|`_itoa`|`_itow`|
134+
|`_ltot`|`_ltoa`|`_ltoa`|`_ltow`|
135+
|`_ultot`|`_ultoa`|`_ultoa`|`_ultow`|
188136
|`_i64tot`|`_i64toa`|`_i64toa`|`_i64tow`|
189137
|`_ui64tot`|`_ui64toa`|`_ui64toa`|`_ui64tow`|
190138

191139
## Requirements
192140

193141
|Routine|Required header|
194142
|-------------|---------------------|
195-
|`_itoa`|\<stdlib.h>|
196-
|`_i64toa`|\<stdlib.h>|
197-
|`_ui64toa`|\<stdlib.h>|
198-
|`_itow`|\<stdlib.h>|
199-
|`_i64tow`|\<stdlib.h>|
200-
|`_ui64tow`|\<stdlib.h>|
143+
|`_itoa`, `_ltoa`, `_ultoa`, `_i64toa`, `_ui64toa`|\<stdlib.h>|
144+
|`_itow`, `_ltow`, `_ultow`, `_i64tow`, `_ui64tow`|\<stdlib.h> or \<wchar.h>|
201145

202-
For more compatibility information, see [Compatibility](../../c-runtime-library/compatibility.md) in the Introduction.
146+
These functions and macros are Microsoft-specific. For more compatibility information, see [Compatibility](../../c-runtime-library/compatibility.md).
203147

204148
## Example
205149

@@ -209,31 +153,32 @@ For more compatibility information, see [Compatibility](../../c-runtime-library/
209153
// This program makes use of the _itoa functions
210154
// in various examples.
211155

156+
#define _CRT_SECURE_NO_WARNINGS 1
212157
#include <string.h>
213158
#include <stdlib.h>
214159

215160
int main( void )
216161
{
217-
char buffer[65];
162+
char buffer[_MAX_U64TOSTR_BASE2_COUNT];
218163
int r;
219-
for( r=10; r>=2; --r )
164+
165+
for ( r = 10; r >= 2; --r )
220166
{
221-
_itoa( -1, buffer, r ); // C4996
222-
// Note: _itoa is deprecated; consider using _itoa_s instead
167+
_itoa( -1, buffer, r );
223168
printf( "base %d: %s (%d chars)\n", r, buffer, strnlen(buffer, _countof(buffer)) );
224169
}
225170
printf( "\n" );
226-
for( r=10; r>=2; --r )
171+
172+
for ( r = 10; r >= 2; --r )
227173
{
228-
_i64toa( -1L, buffer, r ); // C4996
229-
// Note: _i64toa is deprecated; consider using _i64toa_s
174+
_i64toa( -1L, buffer, r );
230175
printf( "base %d: %s (%d chars)\n", r, buffer, strnlen(buffer, _countof(buffer)) );
231176
}
232177
printf( "\n" );
233-
for( r=10; r>=2; --r )
178+
179+
for( r = 10; r >= 2; --r )
234180
{
235-
_ui64toa( 0xffffffffffffffffL, buffer, r ); // C4996
236-
// Note: _ui64toa is deprecated; consider using _ui64toa_s
181+
_ui64toa( 0xffffffffffffffffL, buffer, r );
237182
printf( "base %d: %s (%d chars)\n", r, buffer, strnlen(buffer, _countof(buffer)) );
238183
}
239184
}

0 commit comments

Comments
 (0)