Convert byte stream to user defined type/class

G

Guest

Hi,

My application receives data from multiple sources transferred over
ethernet. This data is broken into packets of bytes before being transmitted
over the network.

My application has to take the data and arrange into a user defined
type/class.

e.g one message from source 1 may be 10-15KB. This is broken down into
smaller packets say for arguments sake 1KB.

My application then has to take each packet and construct the data back into
it's orginal size of 15KB.

This will probably be stored in some sort of array/arraylist of doubles
rather than bytes.

My question is :- what is the best/most efficient way of converting the
individual packets of bytes into an array of say doubles?

Regards
Macca
 
J

Jon Skeet [C# MVP]

Macca said:
My application receives data from multiple sources transferred over
ethernet. This data is broken into packets of bytes before being transmitted
over the network.

My application has to take the data and arrange into a user defined
type/class.

e.g one message from source 1 may be 10-15KB. This is broken down into
smaller packets say for arguments sake 1KB.

My application then has to take each packet and construct the data back into
it's orginal size of 15KB.

This will probably be stored in some sort of array/arraylist of doubles
rather than bytes.

My question is :- what is the best/most efficient way of converting the
individual packets of bytes into an array of say doubles?

It's not clear whether you're only controlling the reading side or not.
Do you get to decide how to write the data *and* how to read it, or is
the data you're reading already in a fixed format?

Jon
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Macca said:
Hi,

My application receives data from multiple sources transferred over
ethernet. This data is broken into packets of bytes before being
transmitted
over the network.

The package size is (in most cases) irrelevant. The network layer is the
responsible of doing this . your app just read X amount of data.
My application has to take the data and arrange into a user defined
type/class.

Serialization could help you here.
My question is :- what is the best/most efficient way of converting the
individual packets of bytes into an array of say doubles?

If you know the size of the array you can read that amount of data in a
byte[], then using BitConverter.ToDouble ( .... ) convert each chunk to a
double.
 
W

William Stacey [MVP]

First, you need to know how many bytes you expect to receive or have some
delimiter to tell you. If both sides are .Net, then the easiest way is to
use BinarySerializer to/from byte[]. It does not matter how many packets
are sent as long as you wait till you receive all bytes before the
conversion.

--
William Stacey [MVP]

| Hi,
|
| My application receives data from multiple sources transferred over
| ethernet. This data is broken into packets of bytes before being
transmitted
| over the network.
|
| My application has to take the data and arrange into a user defined
| type/class.
|
| e.g one message from source 1 may be 10-15KB. This is broken down into
| smaller packets say for arguments sake 1KB.
|
| My application then has to take each packet and construct the data back
into
| it's orginal size of 15KB.
|
| This will probably be stored in some sort of array/arraylist of doubles
| rather than bytes.
|
| My question is :- what is the best/most efficient way of converting the
| individual packets of bytes into an array of say doubles?
|
| Regards
| Macca
|
 
G

Guest

Hi William,

Thanks for the reply. The server side is .NET but the client side is
embedded C.

I will know in advance how much data is being sent.

e.g 10 packets of 256KB which will be sent and then needs to be recompiled
into and array of doubles.

Can BinarySerializer still be used in this scenario. Is this a .NET method?

Thanks In Advance
Macca
 
W

William Stacey [MVP]

1) Get the len bytes (i.e. 4)
2) Get buff[N] bytes.
3) Walk the buffer, converting bytes to doubles (using
BitConverter.ToDouble(buff, indexer)) and putting into a List<double>.
4) Are they the same Endianess? If not, need to make that adjustment.

--
William Stacey [MVP]

| Hi William,
|
| Thanks for the reply. The server side is .NET but the client side is
| embedded C.
|
| I will know in advance how much data is being sent.
|
| e.g 10 packets of 256KB which will be sent and then needs to be
recompiled
| into and array of doubles.
|
| Can BinarySerializer still be used in this scenario. Is this a .NET
method?
|
| Thanks In Advance
| Macca
|
|
|
| "William Stacey [MVP]" wrote:
|
| > First, you need to know how many bytes you expect to receive or have
some
| > delimiter to tell you. If both sides are .Net, then the easiest way is
to
| > use BinarySerializer to/from byte[]. It does not matter how many
packets
| > are sent as long as you wait till you receive all bytes before the
| > conversion.
| >
| > --
| > William Stacey [MVP]
| >
| > | > | Hi,
| > |
| > | My application receives data from multiple sources transferred over
| > | ethernet. This data is broken into packets of bytes before being
| > transmitted
| > | over the network.
| > |
| > | My application has to take the data and arrange into a user defined
| > | type/class.
| > |
| > | e.g one message from source 1 may be 10-15KB. This is broken down into
| > | smaller packets say for arguments sake 1KB.
| > |
| > | My application then has to take each packet and construct the data
back
| > into
| > | it's orginal size of 15KB.
| > |
| > | This will probably be stored in some sort of array/arraylist of
doubles
| > | rather than bytes.
| > |
| > | My question is :- what is the best/most efficient way of converting
the
| > | individual packets of bytes into an array of say doubles?
| > |
| > | Regards
| > | Macca
| > |
| >
| >
| >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top