Avatar of Alias_
Alias_
 asked on

Converting a CString to an int

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
{
i++;
reada = line[i];
//loop number of times the multiplyer requires outputing the character
i++;
multiplyer = line[i];
times = static_cast<int>(multiplyer);
for(j=1;j < times;j++)
{
final = final+reada;
}
}
else
{//No flag means to just output the actual character.
final = final + reada;
}
}
return CString();
C++

Avatar of undefined
Last Comment
Salte

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
curmudgeon42

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
akshayxx

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
1345444441345

Salte

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.

Alf
Alias_

ASKER
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?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Salte

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.

Alf