CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Dec 2004
    Location
    DeathValley
    Posts
    184

    How to convert CString to long?

    Since I am working with sockets, I need data in a string format to be transmitted. While sending, I am converting long values to char/string by using _itoa(). After recieving the data, I need to get it back to the long type. How do I do this?

  2. #2
    Join Date
    Jun 2002
    Posts
    936

    Re: How to convert CString to long?

    can you typcast it to long?

  3. #3
    Join Date
    Dec 2004
    Location
    DeathValley
    Posts
    184

    Re: How to convert CString to long?

    How exactly?

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: How to convert CString to long?

    No, you can't type cast it.

    The topic is covered in the FAQ. Here's a hint. If itoa converts an int to a string, and you want to convert a string to an int, what do you think the appropriate function might be called?

  5. #5
    Join Date
    Jun 2002
    Posts
    936

    Re: How to convert CString to long?

    check out these two function

    strtol and wcstol

  6. #6
    Join Date
    Nov 2001
    Posts
    323

    Re: How to convert CString to long?

    try atoi (ascii to int)
    Best Regards,

    --Zim
    If you find this post useful, please rate it.
    _________________________________
    "Have you the brain worms?!?!?"

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: How to convert CString to long?

    Quote Originally Posted by Zim327
    try atoi (ascii to int)
    If I reember correctly, atoi stands for alphanumeric to integer actually...
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: How to convert CString to long?

    Quote Originally Posted by ur_unholyness
    Since I am working with sockets, I need data in a string format to be transmitted.
    May I ask you why do you need a string to transmit your data? What API are you using? Do you use MFC? CSocket class? (I guess other implementations would be similar).
    I think that you could send ANY data by passing its address and length to a Send() method. Am I wrong?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  9. #9
    Join Date
    Mar 2005
    Posts
    2

    Re: How to convert CString to long?

    ha,
    use atol

  10. #10
    Join Date
    Nov 2001
    Location
    Kerala,India
    Posts
    650

    Re: How to convert CString to long?

    CString strVal = long val;
    char cVal[20];
    wsprintf ( cVal,"%s",strVal );
    long lVal = atol ( cVal );
    lVal;

    Try this

  11. #11
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: How to convert CString to long?

    The answer could have been found in the following FAQ....

  12. #12
    Join Date
    Dec 2004
    Location
    DeathValley
    Posts
    184

    Re: How to convert CString to long?

    Quote Originally Posted by flystar
    ha,
    use atol
    Just what I needed. Thank you everybody for helping me out.

  13. #13
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: How to convert CString to long?

    Ok, although its off-topic, but I would like to ask what VladimirF did, in a different way.

    Consider this.

    Value: 12345.54321

    Now, you don't have to have a Ph.D in maths to figure out that how much size it will take as a string and as a float. In real time application, suppose if you have a tight loop, now before sending, you may convert, any no. of values to a string, then you make a big string, that holds all of them, offcourse sepereated by some delimiter, and then put it on the line.

    Putting bandwidth consumption issue aside, when you get the string at the other end, you tokenize it, then get the initial tokes to figureout what was followed in the string and then now you perform the coutner part, convert back strings to integers/floats etc.

    Depending upon the volume of data being transfered, the performance of the application and bandwidth consumption will be high. Now, you may say...But who cares, if its working then its acceptable...just get the **** job done.

    Well, this is something very serious, this thing will always be a real pain in neck and if you have developed few applications based upon this design, then under normal conditions, with small data transfer, you may not notice much difference, but when this volume will increase, your sufisticated hardware won't be able to help you much and then you may not be able to replace it either, because it involves alot of risk.

    There can be other intelligent ways to optimize it, so that performance and bandwidth consumption don't get compormized, but I won't suggest this solution, unless this is the last thing on earth to live with. Just my 2 cents (hmmm, my be 4...hmm....no 1....**** whatever)

  14. #14
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: How to convert CString to long?

    Quote Originally Posted by ur_unholyness
    Since I am working with sockets, I need data in a string format to be transmitted.
    Working with the sockets you don't need to convert digits to their string representation, at least you're not obliged to. All binary data usually converted to net format (htonl) at sending end and reverted back to host format (ntohl) at receiving end. Converted data are sended as byte sequence of predefined length. That is all you need. Knowing for sure the RX end and TX end have the same endian convention (for example, like all Windows platforms do) you're even not needed to convert data at all and wire them as they are.
    Best regards,
    Igor

  15. #15
    Join Date
    Dec 2004
    Location
    DeathValley
    Posts
    184

    Re: How to convert CString to long?

    My Functions with data transferred as CStrings

    Recieving::
    Code:
     void CSockDlg::OnReceive() 
    
    {
    
    char *pBuf = new char[1025];
    
    int iBufSize = 1024;
    
    int iRcvd;
    
    CString strRecvd;
    
    // Receive the message
    
    iRcvd = m_sConnectSocket.Receive(pBuf, iBufSize);
    
    // Did we receive anything?
    
    if (iRcvd == SOCKET_ERROR)
    
    {
    
    }
    
    else
    
    {
    
    // Truncate the end of the message
    
    pBuf[iRcvd] = NULL;
    
    // Copy the message to a CString
    
    strRecvd = pBuf;
    
    // Add the message to the received list box
    
    m_ctlRecvd.AddString(strRecvd);
    
    // Sync the variables with the controls
    
    UpdateData(FALSE);
    
    }
    
    }
    Sending::
    Code:
     void CSockDlg::OnBsend() 
    
    {
    
    // TODO: Add your control notification handler code here
    
    int iLen;
    
    int iSent;
    
    // Sync the controls with the variables
    
    UpdateData(TRUE);
    
    // Is there a message to be sent?
    
    if (m_sMessage != "")
    
    {
    
    // Get the length of the message
    
    iLen = m_sMessage.GetLength();
    
    // Send the message
    
    iSent = m_sConnectSocket.Send(LPCTSTR(m_sMessage), iLen);
    
    // Were we able to send it?
    
    if (iSent == SOCKET_ERROR)
    
    {
    
    }
    
    else
    
    {
    
    // Add the message to the list box.
    
    m_ctlSent.AddString(m_sMessage);
    
    // Sync the variables with the controls
    
    UpdateData(FALSE);
    
    }
    
    }
    
    }
    How to do the same by transferring data as long values??

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured