Solved

How do I convert a CString to an integer in C++ ?

Posted on 2001-06-13
5
267 Views
Last Modified: 2010-04-02
A C++ code example how to convert a CString to an integer value as simple as possible.
0
Comment
Question by:SusanneJost
  • 3
5 Comments
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6186423

atoi(strTmp)
0
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6186436
and reverse:

strTemp.Format("%i", intValue)
0
 
LVL 86

Expert Comment

by:jkr
ID: 6186482
'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 );
   }
}
0
 
LVL 2

Accepted Solution

by:
missionImpossible earned 50 total points
ID: 6186765
it's as simple as possible.
0
 

Author Comment

by:SusanneJost
ID: 6188275
thanks, much appreciated!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question