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?
Printable View
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?
can you typcast it to long?
How exactly?
No, you can't type cast it. :rolleyes:
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?
check out these two function
strtol and wcstol
try atoi (ascii to int)
If I reember correctly, atoi stands for alphanumeric to integer actually...Quote:
Originally Posted by Zim327
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).Quote:
Originally Posted by ur_unholyness
I think that you could send ANY data by passing its address and length to a Send() method. Am I wrong?
ha,
use atol
CString strVal = long val;
char cVal[20];
wsprintf ( cVal,"%s",strVal );
long lVal = atol ( cVal );
lVal;
Try this
The answer could have been found in the following FAQ....
Just what I needed. Thank you everybody for helping me out.Quote:
Originally Posted by flystar
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) ;)
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.Quote:
Originally Posted by ur_unholyness
My Functions with data transferred as CStrings
Recieving::
Sending::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);
}
}
How to do the same by transferring data as long values??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);
}
}
}