|
-
March 9th, 2005, 03:01 PM
#1
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?
-
March 9th, 2005, 03:12 PM
#2
Re: How to convert CString to long?
can you typcast it to long?
-
March 9th, 2005, 03:15 PM
#3
Re: How to convert CString to long?
-
March 9th, 2005, 03:17 PM
#4
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?
-
March 9th, 2005, 03:21 PM
#5
Re: How to convert CString to long?
check out these two function
strtol and wcstol
-
March 9th, 2005, 03:29 PM
#6
Re: How to convert CString to long?
Best Regards,
--Zim If you find this post useful, please rate it.
_________________________________
"Have you the brain worms?!?!?"
-
March 9th, 2005, 05:28 PM
#7
Re: How to convert CString to long?
 Originally Posted by Zim327
try atoi (ascii to int)
If I reember correctly, atoi stands for alphanumeric to integer actually...
-
March 9th, 2005, 07:13 PM
#8
Re: How to convert CString to long?
 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...
-
March 10th, 2005, 12:33 AM
#9
Re: How to convert CString to long?
-
March 10th, 2005, 02:18 AM
#10
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
-
March 10th, 2005, 03:31 AM
#11
Re: How to convert CString to long?
The answer could have been found in the following FAQ....
-
March 17th, 2005, 12:45 PM
#12
Re: How to convert CString to long?
 Originally Posted by flystar
ha,
use atol
Just what I needed. Thank you everybody for helping me out.
-
March 17th, 2005, 11:02 PM
#13
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)
-
March 18th, 2005, 05:58 AM
#14
Re: How to convert CString to long?
 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
-
March 18th, 2005, 01:32 PM
#15
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??
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|