473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How can I concatenate (or append) a string to a byte[] variable?

I have a string, "-1608" that I need to append to a byte[] variable. How can
I convert the string to a byte[] so I can append it? thanks.
--
Thanks.
Aug 16 '06 #1
1 2681
If you already have a byte array that is big enough (i.e. you make it big
enough ahead of time as opposed to appending) you can just use
http://msdn.microsoft.com/library/de...ytestopic2.asp

byte[] Buffer = new byte[Whatever];
System.Text.Encoding.ASCII.GetBytes(YourString, starindex,
YourString.Length, Buffer, wheretostartwritinginthebytearray);

If not then you would have to copy the original array to a larger array then
use a method such as this ...

ex:

static byte [] AppendToBuffer(byte [] _Buffer, string _String) {
byte [] ret = new byte[_Buffer.Length + _String.Length];
Buffer.BlockCopy(_Buffer, 0, ret, 0, _Buffer.Length);
System.Text.Encoding.ASCII.GetBytes(YourString, 0,
YourString.Length, Buffer, _Buffer,Length);
return ret;
}

If you are doing this alot you should probably be using a different type
(such as a memory stream). Which will handle sizing issues for you (or just
use a larger buffer and keep a seperate length for how much you are using)

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

"Pucca" <Pu***@discussions.microsoft.comwrote in message
news:BB**********************************@microsof t.com...
>I have a string, "-1608" that I need to append to a byte[] variable. How
can
I convert the string to a byte[] so I can append it? thanks.
--
Thanks.

Aug 16 '06 #2

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

Similar topics

3
by: Konrad Mathieu | last post by:
How do I - within in a function - make Javascript recognize a string as a variable name? Thanks and cheers, Konrad
2
by: Alessandro Rossi | last post by:
Hi, I have this problem: In C# i have to concatenate a string "hi Carl" with another string like this """, but C# gives me an error. How can i concatenate a string with the " character? Thank...
5
by: Eric | last post by:
Is there a class that will allow me to append a byte to another? The data that I have is binary so StringBuilder will not work.
0
by: Fritz | last post by:
Hi, My problem is getting a string of variable length from a unmanaged DLL, called by a managed DLL using IJW. In der legacy DLL is declared bool UnmanagerClass::GetData(std::wstring* pString)...
6
by: SStory | last post by:
I need to append to a byte array in a maximum of 256 byte increments. How?? dim x as fullbytearray() dim y as windowbytearray() 'y gets it's bytes (256 or less) need to x=x & y
20
by: Manuel | last post by:
hi, I have a problem, a stupid problem. I can't declare a variable of type byte. The g++ said that i have syntactic error in this line. The code is this: byte * variable; well, i think...
5
by: Generic Usenet Account | last post by:
I have been to recreate a problem that I am having with strings with the trivial code snippet given below. In the trivial code example, I am reading five lines from a data file, each line having...
5
by: da1978 | last post by:
Hi experts, I need to convert a string or a Byte array to a string byte array. Its relatively easy to convert a string to an char array or a byte array but not a STRING byte array. i.e. ...
0
by: chand arora | last post by:
i want to concatenate 4 byte value into 1 float value. i will receive data thorugh the microcontroller in the form of byte.Now i want to concatenate this recived 4 byte value into 1 float value.how...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.