473,749 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

{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.

SWXXXXXXXXXYYYY ZZZZZZZZZZZZZZZ ZZZZZZ

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(testfi le.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[] byteFileNameLen gth // byte array that holds size of file name
string pstrFileName // string that contains the FileName
byte[] byteFileSizeLen gth // 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 6745
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.Enc oding.ASCII.Get Bytes("fileName ");

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"War Eagle" <Wa******@discu ssions.microsof t.com> wrote in message
news:68******** *************** ***********@mic rosoft.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.
SWXXXXXXXXXYYYY ZZZZZZZZZZZZZZZ ZZZZZZ

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(testfi le.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[] byteFileNameLen gth // byte array that holds size of file name
string pstrFileName // string that contains the FileName
byte[] byteFileSizeLen gth // 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******@discu ssions.microsof t.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.co m>
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';
byteFileNameLen gth.CopyTo(txFi nalBuffer, offset); offset +=
byteFileNameLen gth.Length;
offset += Encoding.ASCII. GetBytes(pstrFi leName, 0, pstrFileName.Le ngth,
txFinalBuffer, offset);
byteFileSizeLen gth.CopyTo(txtF inalBuffer, offset); offset +=
byteFileSizeLen gth.Length;
byteFileData.Co pyTo(txtFileBuf fer, offset), offset += byteFileData.Le ngth;

At the end offset holds the length of the packet data. Note that
byteFileNameLen gth 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******@discu ssions.microsof t.com> wrote in message
news:68******** *************** ***********@mic rosoft.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.

SWXXXXXXXXXYYYY ZZZZZZZZZZZZZZZ ZZZZZZ

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(testfi le.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[] byteFileNameLen gth // byte array that holds size of file name
string pstrFileName // string that contains the FileName
byte[] byteFileSizeLen gth // 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.Enc oding.ASCII.Get Bytes("fileName ");

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

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

SWXXXXXXXXXYYYY ZZZZZZZZZZZZZZZ ZZZZZZ

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(testfi le.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[] byteFileNameLen gth // byte array that holds size of file name
string pstrFileName // string that contains the FileName
byte[] byteFileSizeLen gth // 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';
byteFileNameLen gth.CopyTo(txFi nalBuffer, offset); offset +=
byteFileNameLen gth.Length;
offset += Encoding.ASCII. GetBytes(pstrFi leName, 0, pstrFileName.Le ngth,
txFinalBuffer, offset);
byteFileSizeLen gth.CopyTo(txtF inalBuffer, offset); offset +=
byteFileSizeLen gth.Length;
byteFileData.Co pyTo(txtFileBuf fer, offset), offset += byteFileData.Le ngth;

At the end offset holds the length of the packet data. Note that
byteFileNameLen gth 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******@discu ssions.microsof t.com> wrote in message
news:68******** *************** ***********@mic rosoft.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.

SWXXXXXXXXXYYYY ZZZZZZZZZZZZZZZ ZZZZZZ

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(testfi le.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[] byteFileNameLen gth // byte array that holds size of file name
string pstrFileName // string that contains the FileName
byte[] byteFileSizeLen gth // 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******@discu ssions.microsof t.com> wrote in message
news:C9******** *************** ***********@mic rosoft.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';
byteFileNameLen gth.CopyTo(txFi nalBuffer, offset); offset +=
byteFileNameLen gth.Length;
offset += Encoding.ASCII. GetBytes(pstrFi leName, 0, pstrFileName.Le ngth,
txFinalBuffer, offset);
byteFileSizeLen gth.CopyTo(txtF inalBuffer, offset); offset +=
byteFileSizeLen gth.Length;
byteFileData.Co pyTo(txtFileBuf fer, offset), offset += byteFileData.Le ngth;

At the end offset holds the length of the packet data. Note that
byteFileNameLen gth 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******@discu ssions.microsof t.com> wrote in message
news:68******** *************** ***********@mic rosoft.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.
SWXXXXXXXXXYYYY ZZZZZZZZZZZZZZZ ZZZZZZ

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(testfi le.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[] byteFileNameLen gth // byte array that holds size of file name
string pstrFileName // string that contains the FileName
byte[] byteFileSizeLen gth // 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****@games4d otnet.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
3535
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser = serial.Serial()
1
7228
by: Eric Hendriks | last post by:
// In an unmanaged DLL the following function must be called: // int VFGeneralize(const BYTE * const * features); // "features" parameter is supposed to be an array of byte arrays. // function is Marshaled as follows: static extern int VFGeneralize(byte features); In C# I have the following: // Allocate memory to store "Count" references to byte arrays
4
2347
by: Juan | last post by:
Does any one know if there are reported bugs when concatenating strings? When debugging each variable has the correct value but when I try to concatenate them some values are missing (I can´t see them in the debugger). After encoding the string (the sameone which complete value is not visible from the debugger) all the values can be seen but they are spaced by big amounts of zeros and use more that the 2048 bytes allocated. It is like if...
5
29249
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir); pfiles = ld.GetFiles("*.aspx.resx|") + ld.GetFiles("*.ascx.resx") + ld.GetFiles("*.master.resx"); but of course there is no + operation allowed on the FileInfo arrays returned by the GetFiles method.
7
6886
by: Joseph Lee | last post by:
Hi All, I am having problem when i am using hashtable to keep an array of bytes value as keys. Take a look at the code snippet below --------------------------------------------------- ASCIIEncoding asciiEncoder = new ASCIIEncoding();
4
2032
by: Richard L Rosenheim | last post by:
Is there any built-in method or mechanism for concatenating two arrays of byte together? I haven't come across anything to do this, and was just checking before I implement some code. Richard Rosenheim
17
7253
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.