cast cstring type to integer type 
Author Message
 cast cstring type to integer type

CString is a pre-defined class in Visual C++.
Anyone knows how to cast a CString type to standard integer type?
For example:
CString teststr="123";
int a;
how to cast teststr to integer a? let a=123 ?




Wed, 13 Nov 2002 03:00:00 GMT  
 cast cstring type to integer type

Quote:
>Anyone knows how to cast a CString type to standard integer type?
>For example:
>CString teststr="123";
>int a;
>how to cast teststr to integer a? let a=123 ?

You'll have to provide your own operator= to do that - however its not
wise to create operators that do things that aren't intuitive.
Personally I'd just use atoi.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Thu, 14 Nov 2002 03:00:00 GMT  
 cast cstring type to integer type
Read my essay on CStrings on my MVP Tips site.
                        joe

Quote:

>CString is a pre-defined class in Visual C++.
>Anyone knows how to cast a CString type to standard integer type?
>For example:
>CString teststr="123";
>int a;
>how to cast teststr to integer a? let a=123 ?



Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Thu, 14 Nov 2002 03:00:00 GMT  
 cast cstring type to integer type



Quote:
> CString is a pre-defined class in Visual C++.
> Anyone knows how to cast a CString type to standard integer type?
> For example:
> CString teststr="123";
> int a;
> how to cast teststr to integer a? let a=123 ?

Personnaly I use:

inline int NfS(CString s){
 int retour;
 retour=atoi(s.GetBuffer(s.GetLength()));
 s.ReleaseBuffer();
 return retour;

Quote:
}

[I know it could be "optimized" but it works...;-)]

--
Tenebrax
http://graff.ctw.net



Thu, 14 Nov 2002 03:00:00 GMT  
 cast cstring type to integer type
Overkill. You don't need the GetBuffer, or the GetLength, or the
ReleaseBuffer.

_atoi(s)

will do the job perfectly. If you want compatibility with full
Unicode, use

_ttoi(s)
                                joe

On Sun, 28 May 2000 11:40:07 +0200, "Tenebrax"

Quote:



>> CString is a pre-defined class in Visual C++.
>> Anyone knows how to cast a CString type to standard integer type?
>> For example:
>> CString teststr="123";
>> int a;
>> how to cast teststr to integer a? let a=123 ?

>Personnaly I use:

>inline int NfS(CString s){
> int retour;
> retour=atoi(s.GetBuffer(s.GetLength()));
> s.ReleaseBuffer();
> return retour;
>}

>[I know it could be "optimized" but it works...;-)]

Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Thu, 14 Nov 2002 03:00:00 GMT  
 cast cstring type to integer type

Quote:

>CString is a pre-defined class in Visual C++.
>Anyone knows how to cast a CString type to standard integer type?
>For example:
>CString teststr="123";
>int a;
>how to cast teststr to integer a? let a=123 ?

How about subclassing CString and adding a conversion operator,
hopefully my memory serves for the syntax (its been a long time since
I had to add my own conversion operators to a class):

class CMyString : public CString
{
        // Work out the constructor(s) and destructor(s) yourself

        int             operator         int(void);

Quote:
}

int     CMyString::operator int(void)
{
        // I'm not going to tell you how to convert string to int

Quote:
}

Regards,
David.

------------------------------------------------------
In order to avoid the harassment of spam I have
deliberately included an invalid e-mail address in
this message.  To contact me by mail remove the not.

should note that due to the quantity of mail I receive
I may not answer and I am more likely to respond to
follow-up messages.
------------------------------------------------------



Thu, 14 Nov 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Type Casting between Base and Derived Type (Serialization of objects with Type members)

2. Type casting string to integer

3. Puzzle on type casting an integer

4. type casting to / from pointers to incomplete data types

5. type casting to / from pointers to incomplete data types

6. Casting to/from STL types to MFC types

7. Why can't overload type cast to base type (object)

8. HOW to Convert BSTR Type to CString type

9. Type conversion-How can I convert cstring data type to char*

10. Variables and types of type "Type"

11. Proposal -- new integer types [bitfields - let's free them from unions]

12. dealing with many different types of integer!!

 

 
Powered by phpBB® Forum Software