tags:

views:

190

answers:

1

Hello,

I am converting integer number to binary and put it in a header of a data message. For instance the first meesage that arrived, I would convert the counter to binary that take 4 bytes and had the data message, which is a reguler message containning a, b, c etc'.

Here is how I convert the counter :

//This is preparing the counter as binaryint 
nCounterIn = ...;
int nCounterTotalInNetwork = System.Net.IPAddress.HostToNetworkOrder(nCounterIn);
byte[] byteFormat = BitConverter.GetBytes(nCounterTotalInNetwork);

Now the problem is that now in to take a nother string copy the byteFormat to the beginning of a string and in addition to add the string data.

I do that because I want only in the end to write to the file using binary writer

m_brWriter.Write(byteFormat);
m_brWriter.Flush();
+1  A: 

If it's important to write to the stream in single call you can concatenate the arrays:

var intArray = new byte[4];       // In real code assign
var stringArray = new byte[12];   // actual arrays

var concatenated = new byte[16];
intArray.CopyTo(concatenated, 0);
stringArray.CopyTo(concatenated, 4);

m_brWriter.Write(concatenated);
m_brWriter.Flush();

Did you consider writing the arrays in two calls to Write?

Elisha
No must write in one call
Roman Dorevich
"must write in one call" : Why? It is the nature of streams that that doesn't matter. A Stream will buffer and fragment as necessary.
Henk Holterman
I must write in one call, becaue I take in a big buffer a lot ofmessages and then flush them. I know that I can write a few timesan then flush once, But the system is built so I am not allow doing that, it will break the design of the system.
Roman Dorevich