Solved

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

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

744 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

11 Experts available now in Live!

Get 1:1 Help Now