469,926 Members | 1,526 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

{simple?} concatenating byte arrays

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

Nov 16 '05 #1
7 6526
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]
- mv*@spam.guard.caspershouse.com

"War Eagle" <Wa******@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
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

Nov 16 '05 #2
War Eagle <Wa******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
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" <Wa******@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
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

Nov 16 '05 #4
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"War Eagle" <Wa******@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
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


Nov 16 '05 #5
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" wrote:
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" <Wa******@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
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


Nov 16 '05 #6
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" <Wa******@discussions.microsoft.com> wrote in message
news:C9**********************************@microsof t.com...
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" wrote:
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" <Wa******@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
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


Nov 16 '05 #7
Justin Rogers <Ju****@games4dotnet.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

38 posts views Thread by jrlen balane | last post: by
5 posts views Thread by JezB | last post: by
7 posts views Thread by Joseph Lee | last post: by
4 posts views Thread by Richard L Rosenheim | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.