Solved

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

Posted on 2001-06-13
5
266 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

861 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now