DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: int to CString Conversion and vice versa

  1. #1
    Brandon Guest

    int to CString Conversion and vice versa


    Is there any function that will allow me to convert my integers to strings
    and/or strings into integers? I could really use one or two right now.

  2. #2
    AndyH Guest

    Re: int to CString Conversion and vice versa


    "Brandon" <brandonfeil@hotmail.com> wrote:
    >
    >Is there any function that will allow me to convert my integers to strings
    >and/or strings into integers? I could really use one or two right now.


    int nTemp = 10;
    CString cszTemp;

    cszTemp.Format("%d", nTemp); //int to CString

    nTemp = atoi(cszTemp); //CString to int


  3. #3
    Mel Guest

    Re: int to CString Conversion and vice versa


    To convert a CString to an integer try using the following function
    int = atoi(CString). I'm not sure about whether or not there is a function
    that directly goes from int to CSTRing, but you can convert from int to a
    char to a cstring in order to get around it.

    itoa(int ivar,char * cvar,10)where 10 stands for base 10.
    then CString svar = cvar;

    Mel

    "Brandon" <brandonfeil@hotmail.com> wrote:
    >
    >Is there any function that will allow me to convert my integers to strings
    >and/or strings into integers? I could really use one or two right now.



  4. #4
    Paul E. Bible Guest

    Re: int to CString Conversion and vice versa


    "Brandon" <brandonfeil@hotmail.com> wrote:
    >
    >Is there any function that will allow me to convert my integers to strings
    >and/or strings into integers? I could really use one or two right now.



    int nNbr = 5;
    CString strTemp;

    // To CString
    strTemp.Format("%d", nNbr);

    // To Integer
    nNbr = atoi(strTemp);




  5. #5
    Joseph M. Newcomer Guest

    Re: int to CString Conversion and vice versa


    "Brandon" <brandonfeil@hotmail.com> wrote:
    >
    >Is there any function that will allow me to convert my integers to strings
    >and/or strings into integers? I could really use one or two right now.


    Read my essay on CStrings at
    http://www.pgh.net/~newcomer/cstring.htm


  6. #6
    EricB Guest

    Re: int to CString Conversion and vice versa


    "Brandon" <brandonfeil@hotmail.com> wrote:
    >
    >Is there any function that will allow me to convert my integers to strings
    >and/or strings into integers? I could really use one or two right now.


    Yes, by using StringStream classes in C++ <sstream>, and it's ease of use
    beats em all, hands down...it makes type conversions so automatic you don't
    have to worry about memory mgmt or anything...try the following link on DevX:

    http://gethelp.devx.com/techtips/cpp.../10min0401.asp

    Automating Type Conversions with stringstream Objects
    By Danny Kalev, C++ Pro

    Hope this helps! :)
    EB


  7. #7
    Ted Guest

    Re: int to CString Conversion and vice versa


    "EricB" <Netcruiser2@Bellsouth.net> wrote:
    >
    >"Brandon" <brandonfeil@hotmail.com> wrote:
    >>
    >>Is there any function that will allow me to convert my integers to strings
    >>and/or strings into integers? I could really use one or two right now.

    >
    >Yes, by using StringStream classes in C++ <sstream>, and it's ease of use
    >beats em all, hands down...it makes type conversions so automatic you don't
    >have to worry about memory mgmt or anything...try the following link on

    DevX:
    >
    >http://gethelp.devx.com/techtips/cpp.../10min0401.asp
    >
    >Automating Type Conversions with stringstream Objects
    >By Danny Kalev, C++ Pro
    >
    >Hope this helps! :)
    >EB
    >


    I agree that using sstream is great for trivial tasks but there are times
    when ease of use just doesn't cut it. I profiled the sstream method vs.
    and atoi call and the difference is amazing. This is only a single value
    so just imagine the performance hit in a large scale operation. Here are
    the stats:

    with strstream
    Module Statistics for stringtest.exe
    ------------------------------------
    Time in module: 5.013 millisecond
    Percent of time in module: 100.0%
    Functions in module: 937
    Hits in module: 1145
    Module function coverage: 20.8%

    This 20.8 is the actual code that was profiled. the remaining 79.2 was spent
    in sstream.

    with atoi
    Module Statistics for stringtest.exe
    ------------------------------------
    Time in module: 0.006 millisecond
    Percent of time in module: 100.0%
    Functions in module: 5
    Hits in module: 5
    Module function coverage: 100.0%

    You can definately see the time difference as well of the number of calls
    that get made. You can also examine how much time was spent in calls that
    were not profiled that were made to sstream. So this is great for tasks
    that are not performance critical but think again when you have performance
    issues.



  8. #8
    O.V.Ramana Guest

    Re: int to CString Conversion and vice versa



    I will try to give a small example which demonstrates how to convet from
    int to CString and vice-versa.


    1. Converting from int to CString

    int a;
    a=30;
    CString csStr;
    csStr.Format("%d",a);

    MessageBox(csStr);
    now csStr will contain "30".

    2. Converting from CString to int; // i use atoi() function

    CString csStr;
    csStr = "45";
    int b;
    b = atoi(csStr);

    now b will contain the value 30.

    i hope this will help u.

    Ramana.




    "Ted" <TTarney@rens.com> wrote:
    >
    >"EricB" <Netcruiser2@Bellsouth.net> wrote:
    >>
    >>"Brandon" <brandonfeil@hotmail.com> wrote:
    >>>
    >>>Is there any function that will allow me to convert my integers to strings
    >>>and/or strings into integers? I could really use one or two right now.

    >>
    >>Yes, by using StringStream classes in C++ <sstream>, and it's ease of use
    >>beats em all, hands down...it makes type conversions so automatic you don't
    >>have to worry about memory mgmt or anything...try the following link on

    >DevX:
    >>
    >>http://gethelp.devx.com/techtips/cpp.../10min0401.asp
    >>
    >>Automating Type Conversions with stringstream Objects
    >>By Danny Kalev, C++ Pro
    >>
    >>Hope this helps! :)
    >>EB
    >>

    >
    >I agree that using sstream is great for trivial tasks but there are times
    >when ease of use just doesn't cut it. I profiled the sstream method vs.
    >and atoi call and the difference is amazing. This is only a single value
    >so just imagine the performance hit in a large scale operation. Here are
    >the stats:
    >
    >with strstream
    >Module Statistics for stringtest.exe
    >------------------------------------
    > Time in module: 5.013 millisecond
    > Percent of time in module: 100.0%
    > Functions in module: 937
    > Hits in module: 1145
    > Module function coverage: 20.8%
    >
    >This 20.8 is the actual code that was profiled. the remaining 79.2 was

    spent
    >in sstream.
    >
    >with atoi
    >Module Statistics for stringtest.exe
    >------------------------------------
    > Time in module: 0.006 millisecond
    > Percent of time in module: 100.0%
    > Functions in module: 5
    > Hits in module: 5
    > Module function coverage: 100.0%
    >
    >You can definately see the time difference as well of the number of calls
    >that get made. You can also examine how much time was spent in calls that
    >were not profiled that were made to sstream. So this is great for tasks
    >that are not performance critical but think again when you have performance
    >issues.
    >
    >



  9. #9
    Jeff Guest

    Re: int to CString Conversion and vice versa


    "O.V.Ramana" <ramna007@rediffmail.com> wrote:
    >
    >
    > I will try to give a small example which demonstrates how to convet from
    >int to CString and vice-versa.
    >
    >
    >1. Converting from int to CString
    >
    > int a;
    > a=30;
    > CString csStr;
    > csStr.Format("%d",a);
    >
    > MessageBox(csStr);
    > now csStr will contain "30".
    >
    >2. Converting from CString to int; // i use atoi() function
    >
    > CString csStr;
    > csStr = "45";
    > int b;
    > b = atoi(csStr);
    >
    > now b will contain the value 30.
    >
    >i hope this will help u.
    >
    >Ramana.


    Hi Ramana,

    You've already got the right idea, but wrong approach. The simple way would
    be to inherit from the CString class, and create your own class that supports
    this funtionality that you have listed above.



  10. #10
    Ted Guest

    Re: int to CString Conversion and vice versa


    "Jeff" <Jeff@HendriCOM.COM> wrote:
    >
    >"O.V.Ramana" <ramna007@rediffmail.com> wrote:
    >>
    >>
    >> I will try to give a small example which demonstrates how to convet from
    >>int to CString and vice-versa.
    >>
    >>
    >>1. Converting from int to CString
    >>
    >> int a;
    >> a=30;
    >> CString csStr;
    >> csStr.Format("%d",a);
    >>
    >> MessageBox(csStr);
    >> now csStr will contain "30".
    >>
    >>2. Converting from CString to int; // i use atoi() function
    >>
    >> CString csStr;
    >> csStr = "45";
    >> int b;
    >> b = atoi(csStr);
    >>
    >> now b will contain the value 30.
    >>
    >>i hope this will help u.
    >>
    >>Ramana.

    >
    >Hi Ramana,
    >
    >You've already got the right idea, but wrong approach. The simple way would
    >be to inherit from the CString class, and create your own class that supports
    >this funtionality that you have listed above.
    >
    >


    Inheriting from CString for such a trivial task is not a good approach.
    If this inherited class is intended to be used by others or for a more broad
    usage then have at it. But to just do this trivial function, don't waste
    your time.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center