{simple?} concatenating byte arrays

G

Guest

I have two byte arrays and a char (the letter S) I was to concatenate to one byte array. Here is what code I have.
I basically want to send this in a one buffer (byte array?) through a socket.

SWXXXXXXXXXYYYYZZZZZZZZZZZZZZZZZZZZZ

Where S is the command for SEND and should just be the character S.

Where W is a byte representing how long the filename (testfile.txt) is. In this case 12.

Where XXXXXXX is converted from a string that holds the filename(testfile.txt).

Where YYYY is four bytes representing the number of bytes in ZZZZZ etc. 64,0,0,0 in this case.

Where ZZZZZ etc is the actual file (testfile.txt) in bytes.


My overall byte array is TxBuffer.

byte[] byteFileNameLength // byte array that holds size of file name
string pstrFileName // string that contains the FileName
byte[] byteFileSizeLength // byte array that holds size of file


TxBuffer[0] = (byte)'S';

Is there an easy way to fill in the TxBuffer[1 to whatever]?

TIA,
Nate
 
N

Nicholas Paldino [.NET/C# MVP]

War Eagle,

Characters in .NET are unicode, so casting them might cause you to lose
information (or even worse, an overflow exception). What I would recommend
doing is using an Encoder, which will give you a byte array for a
string/array of characters. You seem to want ASCII encoding. You can get
it with this:

// Get the bytes for a particular string.
byte[] pbytBytes = System.Text.Encoding.ASCII.GetBytes("fileName");

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

War Eagle said:
I have two byte arrays and a char (the letter S) I was to concatenate to
one byte array. Here is what code I have.
I basically want to send this in a one buffer (byte array?) through a socket.

SWXXXXXXXXXYYYYZZZZZZZZZZZZZZZZZZZZZ

Where S is the command for SEND and should just be the character S.

Where W is a byte representing how long the filename (testfile.txt) is. In this case 12.

Where XXXXXXX is converted from a string that holds the filename(testfile.txt).

Where YYYY is four bytes representing the number of bytes in ZZZZZ etc. 64,0,0,0 in this case.

Where ZZZZZ etc is the actual file (testfile.txt) in bytes.


My overall byte array is TxBuffer.

byte[] byteFileNameLength // byte array that holds size of file name
string pstrFileName // string that contains the FileName
byte[] byteFileSizeLength // byte array that holds size of file


TxBuffer[0] = (byte)'S';

Is there an easy way to fill in the TxBuffer[1 to whatever]?

TIA,
Nate
 
J

Jon Skeet [C# MVP]

War Eagle said:
I have two byte arrays and a char (the letter S) I was to concatenate
to one byte array. Here is what code I have.
I basically want to send this in a one buffer (byte array?) through a
socket.

Probably the easiest way is to use a MemoryStream - write each bit of
data, and then call ToArray at the end.

On the other hand, if you're sending it through a socket anyway, just
send it in three goes - first the 'S', then the first buffer, then the
second.
 
J

Justin Rogers

byte[] txFinalBuffer = new byte[1500]; // Assuming a pre-allocated array:

int offset = 0;
txFinalBuffer[offset++] = (bytes)'S';
byteFileNameLength.CopyTo(txFinalBuffer, offset); offset +=
byteFileNameLength.Length;
offset += Encoding.ASCII.GetBytes(pstrFileName, 0, pstrFileName.Length,
txFinalBuffer, offset);
byteFileSizeLength.CopyTo(txtFinalBuffer, offset); offset +=
byteFileSizeLength.Length;
byteFileData.CopyTo(txtFileBuffer, offset), offset += byteFileData.Length;

At the end offset holds the length of the packet data. Note that
byteFileNameLength can just be a
byte, since you are only allowing one byte for W anyway, and it doesn't have to
be a byte[].


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

War Eagle said:
I have two byte arrays and a char (the letter S) I was to concatenate to one
byte array. Here is what code I have.
I basically want to send this in a one buffer (byte array?) through a socket.

SWXXXXXXXXXYYYYZZZZZZZZZZZZZZZZZZZZZ

Where S is the command for SEND and should just be the character S.

Where W is a byte representing how long the filename (testfile.txt) is. In this case 12.

Where XXXXXXX is converted from a string that holds the filename(testfile.txt).

Where YYYY is four bytes representing the number of bytes in ZZZZZ etc. 64,0,0,0 in this case.

Where ZZZZZ etc is the actual file (testfile.txt) in bytes.


My overall byte array is TxBuffer.

byte[] byteFileNameLength // byte array that holds size of file name
string pstrFileName // string that contains the FileName
byte[] byteFileSizeLength // byte array that holds size of file


TxBuffer[0] = (byte)'S';

Is there an easy way to fill in the TxBuffer[1 to whatever]?

TIA,
Nate
 
G

Guest

Thanks Nicholas. I will need to do that for the filename. However, how do I take pbyBytes and append it to TxBuffer?

Nate

Nicholas Paldino said:
War Eagle,

Characters in .NET are unicode, so casting them might cause you to lose
information (or even worse, an overflow exception). What I would recommend
doing is using an Encoder, which will give you a byte array for a
string/array of characters. You seem to want ASCII encoding. You can get
it with this:

// Get the bytes for a particular string.
byte[] pbytBytes = System.Text.Encoding.ASCII.GetBytes("fileName");

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

War Eagle said:
I have two byte arrays and a char (the letter S) I was to concatenate to
one byte array. Here is what code I have.
I basically want to send this in a one buffer (byte array?) through a socket.

SWXXXXXXXXXYYYYZZZZZZZZZZZZZZZZZZZZZ

Where S is the command for SEND and should just be the character S.

Where W is a byte representing how long the filename (testfile.txt) is. In this case 12.

Where XXXXXXX is converted from a string that holds the filename(testfile.txt).

Where YYYY is four bytes representing the number of bytes in ZZZZZ etc. 64,0,0,0 in this case.

Where ZZZZZ etc is the actual file (testfile.txt) in bytes.


My overall byte array is TxBuffer.

byte[] byteFileNameLength // byte array that holds size of file name
string pstrFileName // string that contains the FileName
byte[] byteFileSizeLength // byte array that holds size of file


TxBuffer[0] = (byte)'S';

Is there an easy way to fill in the TxBuffer[1 to whatever]?

TIA,
Nate
 
G

Guest

I have two questions for you (and thank you for responding already!). Would there be a problem if I did not pre-allocate memory? When putting the string into the TxBuffer, does the null terminated character get thrown into the buffer?

Nate

Justin Rogers said:
byte[] txFinalBuffer = new byte[1500]; // Assuming a pre-allocated array:

int offset = 0;
txFinalBuffer[offset++] = (bytes)'S';
byteFileNameLength.CopyTo(txFinalBuffer, offset); offset +=
byteFileNameLength.Length;
offset += Encoding.ASCII.GetBytes(pstrFileName, 0, pstrFileName.Length,
txFinalBuffer, offset);
byteFileSizeLength.CopyTo(txtFinalBuffer, offset); offset +=
byteFileSizeLength.Length;
byteFileData.CopyTo(txtFileBuffer, offset), offset += byteFileData.Length;

At the end offset holds the length of the packet data. Note that
byteFileNameLength can just be a
byte, since you are only allowing one byte for W anyway, and it doesn't have to
be a byte[].


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

War Eagle said:
I have two byte arrays and a char (the letter S) I was to concatenate to one
byte array. Here is what code I have.
I basically want to send this in a one buffer (byte array?) through a socket.

SWXXXXXXXXXYYYYZZZZZZZZZZZZZZZZZZZZZ

Where S is the command for SEND and should just be the character S.

Where W is a byte representing how long the filename (testfile.txt) is. In this case 12.

Where XXXXXXX is converted from a string that holds the filename(testfile.txt).

Where YYYY is four bytes representing the number of bytes in ZZZZZ etc. 64,0,0,0 in this case.

Where ZZZZZ etc is the actual file (testfile.txt) in bytes.


My overall byte array is TxBuffer.

byte[] byteFileNameLength // byte array that holds size of file name
string pstrFileName // string that contains the FileName
byte[] byteFileSizeLength // byte array that holds size of file


TxBuffer[0] = (byte)'S';

Is there an easy way to fill in the TxBuffer[1 to whatever]?

TIA,
Nate
 
J

Justin Rogers

No, GetBytes does not append a null string, it only converts the characters
directly into bytes.
As for memory pre-allocations, if you want to send a single byte[] then you'll
need to allocate
a buffer to hold the concatenation of all the others. This can be pre-allocated
like above, or
you can allocate them as needed and size them precisely to the size needed to
hold all
components.

Jon pointed out using the NetworkStream to simply write the components in order.
This is
always a possibility as well. If your data is always going to be less than the
MTU of a
packet though, then it would definitely benefit you to write to the temporary
buffers and
write out entire byte arrays of data in one shot.

Note that constructing the data on the opposite end of the pipe will probably be
more
difficult in your case than sending it there. Your Read methods are going to
have to
deconstruct the data format appropriately.


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

War Eagle said:
I have two questions for you (and thank you for responding already!). Would
there be a problem if I did not pre-allocate memory? When putting the string
into the TxBuffer, does the null terminated character get thrown into the
buffer?
Nate

Justin Rogers said:
byte[] txFinalBuffer = new byte[1500]; // Assuming a pre-allocated array:

int offset = 0;
txFinalBuffer[offset++] = (bytes)'S';
byteFileNameLength.CopyTo(txFinalBuffer, offset); offset +=
byteFileNameLength.Length;
offset += Encoding.ASCII.GetBytes(pstrFileName, 0, pstrFileName.Length,
txFinalBuffer, offset);
byteFileSizeLength.CopyTo(txtFinalBuffer, offset); offset +=
byteFileSizeLength.Length;
byteFileData.CopyTo(txtFileBuffer, offset), offset += byteFileData.Length;

At the end offset holds the length of the packet data. Note that
byteFileNameLength can just be a
byte, since you are only allowing one byte for W anyway, and it doesn't have to
be a byte[].


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

War Eagle said:
I have two byte arrays and a char (the letter S) I was to concatenate to
one
byte array. Here is what code I have.
I basically want to send this in a one buffer (byte array?) through a socket.

SWXXXXXXXXXYYYYZZZZZZZZZZZZZZZZZZZZZ

Where S is the command for SEND and should just be the character S.

Where W is a byte representing how long the filename (testfile.txt) is.
In
this case 12.
Where XXXXXXX is converted from a string that holds the filename(testfile.txt).

Where YYYY is four bytes representing the number of bytes in ZZZZZ etc. 64,0,0,0 in this case.

Where ZZZZZ etc is the actual file (testfile.txt) in bytes.


My overall byte array is TxBuffer.

byte[] byteFileNameLength // byte array that holds size of file name
string pstrFileName // string that contains the FileName
byte[] byteFileSizeLength // byte array that holds size of file


TxBuffer[0] = (byte)'S';

Is there an easy way to fill in the TxBuffer[1 to whatever]?

TIA,
Nate
 
J

Jon Skeet [C# MVP]

Justin Rogers said:
Jon pointed out using the NetworkStream to simply write the
components in order. This is always a possibility as well. If your
data is always going to be less than the MTU of a packet though, then
it would definitely benefit you to write to the temporary buffers and
write out entire byte arrays of data in one shot.

Are you sure? I'd expect it to wait a short time before actuallysending
the data, in order to see if it could get any more in the packet. Of
course, that depends on whether or not Nagle's algorithm is turned on.
 

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