Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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_

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] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

571 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