Converting a CString to an int

Posted on 2003-02-18
Medium Priority
Last Modified: 2007-12-19
I am trying to convert a CString to an int for use in a for loop, this should be simple, but I am new to Visual C++ and only a novice programmer, any help would be greatly appreciated.

This function is looking at a string of compressed data similar to this ( 1345!451345) the important information in the string is the !45 which means ! = flag 4 is the number and 5 is the multiplyer, after it is decompressed it would look like this 44444, total string 1345444441345.

Hope this is enough info.

// The expanding algorithm that will expand the data
CString CRLEView::expand(CString line)
int length = line.GetLength();
int i;
int j;
int times;
CString reada;
CString final;
char multiplyer;
for (i=0; i < length; i++)
{//reading characters one at a time
reada = line[i];
if (reada = "!") //Encounters the flag
reada = line[i];
//loop number of times the multiplyer requires outputing the character
multiplyer = line[i];
times = static_cast<int>(multiplyer);
for(j=1;j < times;j++)
final = final+reada;
{//No flag means to just output the actual character.
final = final + reada;
return CString();
Question by:Alias_
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

curmudgeon42 earned 200 total points
ID: 7979589
Use atoi().  Note that you must put

#include <stdlib.h>

in your program in order to use this function.  It takes a C-String and returns an int.  Sounds like exactly what you need.  Detailed information is at the link below:


Good luck.


Expert Comment

ID: 7979824
u have to make sure that the expanded string corresponds to some integer value, within the limits of MAXINT, ( unsigned !)
other wise u'll see weird results
for 32 bit integers MAXINT is
4294967295 ..  which is much less than ur example of

LVL 12

Expert Comment

ID: 7980759
There's a function StrToInt() in the MFC that convert a CString to an int.

You can also use atoi() but I can't think of any reason why atoi() should be preferred over strtol(). atoi(str) is identical with either strtol(str,0,0) or strtol(str,0,10) - I forgot which one and I really couldn't care, I never use atoi() myself :-)

strtol() is the only real alternative to StrToInt(). I am not here specifically thinking of strtol() alone but the whole family of strtol() like functions:

strtol(), strtoul(), wcstol(), wcstoul(), strtou64(), strtoi64(), wcstou64(), wcstoi64().

All of these functions do the same thing, they differ in what type of strings they take and in what type of result they return:

wcs... functions take wchar_t strings while str... functions take char strings.

...toT() and ...touT() differ in that the ..tou.. return an unsigned integer while the ...to.. return a signed integer.

l means 'long' and is the same as 'int' in C++ under windows. 'i64' is __int64 and is an 8 byte integer while 'u64' is for unsigned __int64.

They all take this signature:

int strtol(const char * str, char ** endp, int radix);

return value is the number returned and is of the type mentioned earlier. the str and endp has types depending on the str... or wcs... functions, it is wchar_t for the wcs..

str is a pointer to the input string, "123" or "235xyz" can be strings that are input to the function.

endp is a pointer to a string pointer. If this value is NULL or 0 then it is ignored. If it is not null then it points to a string pointer and that string pointer is modified so that after the function call the pointer will point into the input string and it will point to the first character that wasn't accepted as part of the number. If input string was "123" then endp will be set to point to the terminating null byte and if input string was "235xyz" then endp will be set to point to the "xyz" part after the number read.

radix is the radix of the number. 10 means decimal, 2 is binary, 8 is octal and 16 is hex. Radix can be any value from 2 up to and including 36.

In addition radix can have the special value 0 which is such that it examines the first part of the number to determine the actual radix.

If the string start with 0x or 0X the string is considered hex and radix 16 is used. Otherwise if the string start with 0 it is considered octal and if neither 0x, 0X nor 0 is used the number is decimal.

One drawback with the functions is that they do not check for overflow, strtol() will happily accept a string like "1243423423413241234122341234123412531435326572" and will return you the value above mod pow(2,32), i.e. the lower 32 bits will be correct but the higher more significant bits will be ignored.


Author Comment

ID: 7990474
I did find this function earlier but had no luck in getting it to work, after reading the attached article it became a little more clear.

I want to thank all of you for your comments and time.

P.S. - I did try StrToInt() and had no luck at all, is it part of C++.NET?
LVL 12

Expert Comment

ID: 7998149
No, StrToInt() is part of MFC. CString doesn't exist in .NET it is replaced with a much better string type in the managed code. Unmanaged code still have to use other type of string and std::string stand out as the best since it's standard. CString can be used if you write MFC code though.


Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
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.
Suggested Courses

764 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