Data Conversion These routines convert data from one form to another. Generally these routines execute faster than conversions you might write. Each routine that begins with a to prefix is implemented as a function and as a macro. See Choosing Between Functions and Macros for information about choosing an implementation. Data-Conversion Routines Routine
Use
abs
Find absolute value of integer
atof
Convert string to float
atoi, _atoi64
Convert string to int
atol
Convert string to long
_ecvt
Convert double to string of specified length
_fcvt
Convert double to string with specified number of digits following decimal point
_gcvt
Convert double number to string; store string in buffer
_itoa, _i64toa, _itow, _i64tow
Convert int to string
labs
Find absolute value of long integer
_ltoa, _ltow
Convert long to string
_mbbtombc
Convert 1-byte multibyte character to corresponding 2-byte multibyte character
_mbcjistojms
Convert Japan Industry Standard (JIS) character to Japan Microsoft (JMS) character
_mbcjmstojis
Convert JMS character to JIS character
_mbctohira
Convert multibyte character to 1-byte hiragana code
_mbctokata
Convert multibyte character to 1-byte katakana code
_mbctombb
Convert 2-byte multibyte character to corresponding 1-byte multibyte character
mbstowcs
Convert sequence of multibyte characters to corresponding sequence of wide characters
mbtowc
Convert multibyte character to corresponding wide character
strtod, wcstod
Convert string to double
strtol, wcstol
Convert string to long integer
strtoul, wcstoul
Convert string to unsigned long integer
strxfrm, wcsxfrm
Transform string into collated form based on locale-specific information
__toascii
Convert character to ASCII code
tolower, towlower, _mbctolower
Test character and convert to lowercase if currently uppercase
_tolower
Convert character to lowercase unconditionally
toupper, towupper, _mbctoupper
Test character and convert to uppercase if currently lowercase
_toupper
Convert character to uppercase unconditionally
_ultoa, _ultow
Convert unsigned long to string
wcstombs
Convert sequence of wide characters to corresponding sequence of multibyte characters
wctomb
Convert wide character to corresponding multibyte character
_wtoi
Convert wide-character string to int
_wtol
Convert wide-character string to long
Send feedback to MSDN. Look here for MSDN Online resources.
atof, atoi, _atoi64, atol Convert strings to double (atof), integer (atoi, _atoi64), or long (atol). double atof( const char *string ); int atoi( const char *string ); __int64 _atoi64( const char *string ); long atol( const char *string ); Routine
Required Header
Compatibility
atof
and ANSI, Win 95, Win NT
atoi
ANSI, Win 95, Win NT
_atoi64
Win 95, Win NT
atol
ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB
Single thread static library, retail version
LIBCMT.LIB
Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each function returns the double, int, __int64 or long value produced by interpreting the input characters as a number. The return value is 0 (for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input cannot be converted to a value of that type. The return value is undefined in case of overflow. Parameter string String to be converted Remarks These functions convert a character string to a double-precision floating-point value (atof), an integer value (atoi and _atoi64), or a long integer value (atol). The input string is a sequence of characters that can be interpreted as a numerical value of the specified type. The output value is affected by the setting of the LC_NUMERIC category in the current
locale. For more information on the LC_NUMERIC category, see setlocale. The longest string size that atof can handle is 100 characters. The function stops reading the input string at the first character that it cannot recognize as part of a number. This character may be the null character ('\0') terminating the string. The string argument to atof has the following form: [whitespace] [sign] [digits] [.digits] [ {d | D | e | E }[sign]digits] A whitespace consists of space and/or tab characters, which are ignored; sign is either plus (+) or minus ( – ); and digits are one or more decimal digits. If no digits appear before the decimal point, at least one must appear after the decimal point. The decimal digits may be followed by an exponent, which consists of an introductory letter ( d, D, e, or E) and an optionally signed decimal integer. atoi, _atoi64, and atol do not recognize decimal points or exponents. The string argument for these functions has the form: [whitespace] [sign]digits where whitespace, sign, and digits are exactly as described above for atof. Generic-Text Routine Mappings TCHAR.H Routine
_UNICODE & _MBCS Not Defined
_MBCS Defined
_UNICODE Defined
_ttoi
atoi
atoi
_wtoi
_ttol
atol
atol
_wtol
Example /* ATOF.C: This program shows how numbers stored * as strings can be converted to numeric values * using the atof, atoi, and atol functions. */ #include #include void main( void ) { char *s; double x; int i; long l; s = " -2309.12E-15"; x = atof( s );
/* Test of atof */
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x ); s = "7.8912654773d210"; /* Test of atof */ x = atof( s ); printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x ); s = " -9885 pigs";
/* Test of atoi */
i = atoi( s ); printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i ); s = "98854 dollars";
/* Test of atol */
l = atol( s ); printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l ); }
Output atof test: ASCII string: -2309.12E-15 float: -2.309120e-012 atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210 atoi test: ASCII string: -9885 pigs atol test: ASCII string: 98854 dollars
integer: -9885 long: 98854
Data Conversion Routines | Floating-Point Support Routines | Locale Routines See Also _ecvt, _fcvt, _gcvt, setlocale, strtod, wcstol, strtoul Send feedback to MSDN. Look here for MSDN Online resources.
strchr, wcschr, _mbschr Find a character in a string. char *strchr( const char *string, int c ); wchar_t *wcschr( const wchar_t *string, wint_t c ); unsigned char *_mbschr( const unsigned char *string, unsigned int c ); Routine
Required Header
Compatibility
strchr
ANSI, Win 95, Win NT
wcschr
or ANSI, Win 95, Win NT
_mbschr
Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB
Single thread static library, retail version
LIBCMT.LIB
Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns a pointer to the first occurrence of c in string, or NULL if c is not found. Parameters string Null-terminated source string c Character to be located Remarks The strchr function finds the first occurrence of c in string, or it returns NULL if c is not found. The null-terminating character is included in the search. wcschr and _mbschr are wide-character and multibyte-character versions of strchr. The arguments and return value of wcschr are wide-character strings; those of _mbschr are multibyte-character strings. _mbschr recognizes multibyte-character sequences according
to the multibyte code page currently in use. These three functions behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine
_UNICODE & _MBCS Not Defined
_MBCS Defined
_UNICODE Defined
_tcschr
strchr
_mbschr
wcschr
Example /* STRCHR.C: This program illustrates searching for a character * with strchr (search forward) or strrchr (search backward). */ #include #include int ch = 'r'; char string[] = "The quick brown dog jumps over the lazy fox"; char fmt1[] = "
1
2
3
4
5";
char fmt2[] = "12345678901234567890123456789012345678901234567890"; void main( void ) { char *pdest; int result; printf( "String to be searched: \n\t\t%s\n", string ); printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 ); printf( "Search char:\t%c\n", ch ); /* Search forward. */ pdest = strchr( string, ch ); result = pdest - string + 1; if( pdest != NULL ) printf( "Result:\tfirst %c found at position %d\n\n", ch, result ); else printf( "Result:\t%c not found\n" );
/* Search backward. */ pdest = strrchr( string, ch ); result = pdest - string + 1; if( pdest != NULL ) printf( "Result:\tlast %c found at position %d\n\n", ch, result ); else printf( "Result:\t%c not found\n" ); }
Output String to be searched: The quick brown dog jumps over the lazy fox 1
2
3
4
5
12345678901234567890123456789012345678901234567890 Search char: r Result: first r found at position 12 Result: last r found at position 30
String Manipulation Routines See Also strcspn, strncat, strncmp, strncpy, _strnicmp, strpbrk, strrchr, strstr Send feedback to MSDN. Look here for MSDN Online resources.