Thursday, February 3, 2011

atof, _wtof, atoi, _wtoi, _atoi64, _wtoi64, atol, _wtol, _ttoi

Convert a string to double (atof and _wtof), integer (atoi, _atoi64, _wtoi and _wtoi64), or long integer (atol and _wtol).


double atof( const char *string );
double _wtof(const wchar_t *string );
int atoi(const char *string );
__int64 _atoi64(const char *string );
int _wtoi(const wchar_t *string );
__int64 _wtoi64(const wchar_t *string );
long atol(const char *string );
long _wtol(const wchar_t *string );
 
Parameters :  string (String to be converted.)

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, _atoi64, _wtoi, and _wtoi64), 0L (for atol and _wtol), or 0.0 (for atof and _wtof) if the input cannot be converted to a value of that type. The return value is undefined in case of overflow.

Remarks
These functions convert a character string to a double-precision, floating-point value (atof and _wtof), an integer value (atoi, _atoi64, _wtoi and _wtoi64), or a long integer value (atol and _wtol).

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 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' or L'\0') terminating the string.

The string argument to atof and _wtof has the following form:
[whitespace] [sign] [digits] [.digits] [ {d D e E }[sign]digits]

A whitespace consists of space 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, atol, _wtoi, _wtoi64 and _wtol 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 as described for atof and _wtof.

Example
// crt_atof.c

#include
#include

int main( void )
{
   char *s; double x; int i; long l;
   s = " -2309.12E-15"; /* Test of atof */

   x = atof( s );

   printf( "atof test: \"%s\"; float: %e\n", s, x );
    s = "7.8912654773d210"; /* Test of atof */

   x = atof( s );
  printf( "atof test: \"%s\"; float: %e\n", s, x );
  s = " -9885 pigs"; /* Test of atoi */
  i = atoi( s );

printf( "atoi test: \"%s\"; integer: %d\n", s, i );
s = "98854 dollars"; /* Test of atol */

l = atol( s );
printf( "atol test: \"%s\"; long: %ld\n", s, l );
}
 
OutPut
atof test: " -2309.12E-15"; float: -2.309120e-012

atof test: "7.8912654773d210"; float: 7.891265e+210
atoi test: " -9885 pigs"; integer: -9885
atol test: "98854 dollars"; long: 98854
 
QA:
How can you multiply  2 numeric values within 2 separate CString variables . How to get the value of the 2 values multiplied together if they are CStrings?
 
Answers
CString s1 ="22";

CString s2="33";

int nResult = atoi(s1)*atoi(s2);
You can use atof too (if are float or double).

Example 2
CString s1 = "22";

CString s2 = "33";
int n1 = atoi( (LPCTSTR) s1);
int n2 = atoi( (LPCTSTR) s2);
int nRes = n1 * n2;

Example 3
int nResult = atoi((LPCTSTR)s1)*atoi((LPCTSTR)s2);


Example 4
When you are using unicode app if CString is defined as unsigned short*

Then use  _wtoi() instead of atoi()

Example 5 (_ttoi )
When using CString, you should not use atoi or _wtoi.


Instead you should use _ttoi instead.

Example:

int nResult = _ttoi(s1)*_ttoi(s2);

The reason _ttoi is better, because then you can easily change your envirnment from UNICODE to ANSI, and back, with out having to change your source code.

_ttoi will automatically use the correct function for the current CString type.

In UNICODE CString uses WIDE charactors, and in ANSI CString uses normal ANSI single byte charactors.
So in practice, you should avoid using any of the ANSI string functions with CString, and use the TCHAR types instead.

Examples:
_tcsrev _strrev
_tcscpy strcpy
_tcscmp strcmp
_tcscat strcat

Note:
By default the VC++ applications are Multiple-byte aplications. Thereis a preprocessor #define directive which defines the symbol _MBCS (see project menu, settings, C/C++ tab)


You can make an application Unicode just removing the _MBCS symbol and adding _UNICODE symbol.
Depending on _UNICODE is defined or not CString (which is TCHAR*) is defined in one or other way. TCHAR is char* in _MBCS but is wchar_t* (unisigned short*) with _UNICODE.

To comple make your app unicode you must In the Output category of the Link tab in the Project Settings dialog box, set the Entry Point Symbol to wWinMainCRTStartup.

Using TCHAR still allows CString to have all its functions working. Actually, I think CString is based on TCHAR to work in both ANSII and UNICODE modes.


Then to convert you result back to a string, just us the format function:
myString.Format("%i", iResult);
After that you can use it to set your window text.

Please provide your comments...

Also Visit:


http://journeyallover.blogspot.com/2010/04/way-to-south-korea.html
http://journeyallover.blogspot.com/2010/04/frequently-asked-interview-question-in.html

http://journeyallover.blogspot.com/2010/04/biometrics.html


No comments:

Post a Comment

Health Benefits of Cashews

  Benefits of Cashews. Healthy food is an integral part of healthy body. Regular exercises such as Yoga and healthy diet is important to...