Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

828 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