SusanneJost
asked on
How do I convert a CString to an integer in C++ ?
A C++ code example how to convert a CString to an integer value as simple as possible.
and reverse:
strTemp.Format("%i", intValue)
strTemp.Format("%i", intValue)
'atoi()' is indeed an option, but does not really give you the necessary error-checking:
#include <stdlib.h>
int i;
char* pc;
CString str ( "1234");
i = (int) strtol ( ( LPCTSTR) str, &pc, 10);
if ( *pc)
{
// error if conversion stopped on something other
// that the null-terminator
}
Here's a more complete sample on conversion in general:
/* STRTOD.C: This program uses strtod to convert a
* string to a double-precision value; strtol to
* convert a string to long integer values; and strtoul
* to convert a string to unsigned long-integer values.
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char *string, *stopstring;
double x;
long l;
int base;
unsigned long ul;
string = "3.1415926This stopped it";
x = strtod( string, &stopstring );
printf( "string = %s\n", string );
printf(" strtod = %f\n", x );
printf(" Stopped scan at: %s\n\n", stopstring );
string = "-10110134932This stopped it";
l = strtol( string, &stopstring, 10 );
printf( "string = %s", string );
printf(" strtol = %ld", l );
printf(" Stopped scan at: %s", stopstring );
string = "10110134932";
printf( "string = %s\n", string );
/* Convert string using base 2, 4, and 8: */
for( base = 2; base <= 8; base *= 2 )
{
/* Convert the string: */
ul = strtoul( string, &stopstring, base );
printf( " strtol = %ld (base %d)\n", ul, base );
printf( " Stopped scan at: %s\n", stopstring );
}
}
#include <stdlib.h>
int i;
char* pc;
CString str ( "1234");
i = (int) strtol ( ( LPCTSTR) str, &pc, 10);
if ( *pc)
{
// error if conversion stopped on something other
// that the null-terminator
}
Here's a more complete sample on conversion in general:
/* STRTOD.C: This program uses strtod to convert a
* string to a double-precision value; strtol to
* convert a string to long integer values; and strtoul
* to convert a string to unsigned long-integer values.
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char *string, *stopstring;
double x;
long l;
int base;
unsigned long ul;
string = "3.1415926This stopped it";
x = strtod( string, &stopstring );
printf( "string = %s\n", string );
printf(" strtod = %f\n", x );
printf(" Stopped scan at: %s\n\n", stopstring );
string = "-10110134932This stopped it";
l = strtol( string, &stopstring, 10 );
printf( "string = %s", string );
printf(" strtol = %ld", l );
printf(" Stopped scan at: %s", stopstring );
string = "10110134932";
printf( "string = %s\n", string );
/* Convert string using base 2, 4, and 8: */
for( base = 2; base <= 8; base *= 2 )
{
/* Convert the string: */
ul = strtoul( string, &stopstring, base );
printf( " strtol = %ld (base %d)\n", ul, base );
printf( " Stopped scan at: %s\n", stopstring );
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks, much appreciated!
atoi(strTmp)