Help on String Conversions

This is a discussion on Help on String Conversions within the C++ Programming forums, part of the General Programming Boards category; Hi, i am using eVC++( client side) to get some input from the dialog and send it to a server ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    Help on String Conversions

    Hi,

    i am using eVC++( client side) to get some input from the dialog and send it to a server running as a console application in VC++. I have 2 edit boxes with one of the m having a mapping to a CString object and the other mapped to an integer value.
    I need to pack these 2 values and send it as a CString object to my Send function.

    I need to convert my integer valriable m_value to CString object so that i can concatenate the 2 CStrings.

    What i tried to was :
    CString m_strMessage,m_str;
    CString str;
    char* value="";
    value=(char *)&m_value;
    mbstowcs(str,value,sizeof(value));

    m_strMessage = m_str + str;
    // Send message to server
    UINT uiResult = Send(m_strMessage);

    The program gave the following error:
    error C2664: 'mbstowcs' : cannot convert parameter 1 from 'class CString' to 'unsigned short *'

    Can someone please help me on how to convert an integer value to a CString object.

    Thanks,
    Prasad

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    161
    I'm not a fan of MFC, so I don't know much about CString. But, assuming that CString has a constructor accepting a const char*, you can try this:

    Code:
    #include <sstream>
    
    // ....
    
    CString number_to_string(int number)
    {
      std::ostringstream oss; 
      oss << number;
    
      return CString(oss.str().c_str());
    }

  3. #3
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    You need to use GetBuffer on the str variable to recieve a pointer to the char* contained within. Then use ReleaseBuffer to release the previously allocated buffer from GetBuffer. That may solve your problem.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    2
    Hi,

    i found the way to do it. i used the Format method of the CString Class.

    for example to convert to integers i used:

    CString str;
    int i=15;

    str.Format(_T("%d"), i);

    It worked for me.

    Thanx for the replies. i really appreciate your help.

    -Prasad

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21