Converting different data types from a socket. 
Author Message
 Converting different data types from a socket.

I am trying to convert the bytes receive from a CAsyncSocket.  The data
I am receiving will always be in a standard message format.  the first 8
bytes of data are a header, followed by data.

My question is: how do I convert the bytes received into UINT, char...
etc.
I know that the first 4 bytes make up a UINT.  and the second 4 bytes
make up UINT.  the remaining 5 bytes are char...

the raw data looks like this:
0.0.0.16.0.0.1.1.97.98.99.100.101
should be
|    16    |   255   | a | b | c  |  d  |  e  |

how do I extract the first and second 4 bytes into UINT?

OnReceive()
{
    BYTE *pBuf = new BYTE[1025];
    int iBufSize = 1024;
    int iRcvd = 0;

    iRcvd = MySocket.Receive(pBuf, 1024);

---->>   now..  that the data is stored in pBuf, how do I use the first
4 bytes to = UINT?

the char part is easy.

char cdata[0] = pBuf[8];   etc.

Quote:
}

any help would be appreciated.


Fri, 28 Jun 2002 03:00:00 GMT  
 Converting different data types from a socket.

Quote:

> I am trying to convert the bytes receive from a CAsyncSocket.  The data
> I am receiving will always be in a standard message format.  the first 8
> bytes of data are a header, followed by data.

> My question is: how do I convert the bytes received into UINT, char...
> etc....

Define a struct and cast the received bytes into it:

struct header {
UINT one;
UINT two;
char c1;
char c2;

Quote:
};

header* pheader = (header*)pBuf;
UINT result1 = pheader->one;
UINT result2 = pheader->two;

--
Scott McPhillips [VC++ MVP]



Fri, 28 Jun 2002 03:00:00 GMT  
 Converting different data types from a socket.
Thanks for the help Scott.  you have pointed me in the right direction.
Quote:


> > I am trying to convert the bytes receive from a CAsyncSocket.  The data
> > I am receiving will always be in a standard message format.  the first 8
> > bytes of data are a header, followed by data.

> > My question is: how do I convert the bytes received into UINT, char...
> > etc....

> Define a struct and cast the received bytes into it:

> struct header {
> UINT one;
> UINT two;
> char c1;
> char c2;
> };

> header* pheader = (header*)pBuf;
> UINT result1 = pheader->one;
> UINT result2 = pheader->two;

> --
> Scott McPhillips [VC++ MVP]



Sat, 29 Jun 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. converting to different data types

2. Different value when converting into different type

3. Type conversion-How can I convert cstring data type to char*

4. converting BSTR data type to common string type!

5. System.Data.SqlClient.SqlException: Error converting data type varchar to numeric

6. getting CONVERT function info different data sources

7. getting CONVERT function info different data sources

8. conversion between different data type

9. Different Data Types in a File

10. Templates with different data types.

11. Array of different data types.

12. ATL data type for VB Object data Type

 

 
Powered by phpBB® Forum Software