473,386 Members | 1,720 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.

ByteArray to String


What is the best way to convert a bytearray into a string ?

I thought I could do it using the code below, but is there anything
faster/easier than this? Thanks!

Chris

int aLength = 100000;
byte[] myByteArray = new byte[aLength ];
string myString = "";
for (int i=0; i<aLength ; i++)
{
myString += (char) myByteArray[i];
}

//work with this new string: cut out every char=='A'
myString.split('A');

....

Nov 17 '05 #1
5 2218
I've been doing this is VC# Express 2005 Beta:

ASCIIEncoding AE = new ASCIIEncoding();
string TextString = AE.GetString(byte-array);

Don't know if it works in other versions, so you'll need to check in your
specific VS release.

HTH
Steve
"Chris" <no****@nospam.be> wrote in message
news:7b********************************@4ax.com...

What is the best way to convert a bytearray into a string ?

I thought I could do it using the code below, but is there anything
faster/easier than this? Thanks!

Chris

int aLength = 100000;
byte[] myByteArray = new byte[aLength ];
string myString = "";
for (int i=0; i<aLength ; i++)
{
myString += (char) myByteArray[i];
}

//work with this new string: cut out every char=='A'
myString.split('A');

...

Nov 17 '05 #2
Chris,

Do you know the encoding of the string? If so, then use the appropriate
encoder class (AsciiEncoder, UnicodeEncoder, etc, etc).

You would just call the GetString method on the encoder class at that
point.

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

"Chris" <no****@nospam.be> wrote in message
news:7b********************************@4ax.com...

What is the best way to convert a bytearray into a string ?

I thought I could do it using the code below, but is there anything
faster/easier than this? Thanks!

Chris

int aLength = 100000;
byte[] myByteArray = new byte[aLength ];
string myString = "";
for (int i=0; i<aLength ; i++)
{
myString += (char) myByteArray[i];
}

//work with this new string: cut out every char=='A'
myString.split('A');

...

Nov 17 '05 #3
First of all you are assuming that your array of bytes is an array of
chars, which might be risky.

However, String has a constructor that takes an array of characters.

Nov 17 '05 #4
Chris wrote:
What is the best way to convert a bytearray into a string ?


That all depends on the source of the byte array. What kind of data
does it represent? If it's text data, what encoding was used to encode
it? If it's arbitrary binary data (eg an mp3 file) you need to use
something like base64.

See http://www.pobox.com/~skeet/csharp/unicode.html

By the way, concatenating strings in a loop like that is a
performance-killer. See
http://www.pobox.com/~skeet/csharp/stringbuilder.html for more
information.

Jon

Nov 17 '05 #5
Thank you very much for these fast replies!

Chris

Nov 17 '05 #6

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

Similar topics

0
by: Kristina Engdahl | last post by:
Hello, I'm working with dynamic classloading and have encountered a problem when it comes to defining an URL for a file that is available through a bytearray representing a jar-file. The file in...
2
by: Lou | last post by:
How do I convert a byteArray to a string?
13
by: Ekim | last post by:
hy to all, I'm accessing a function from a managed-c++-dll within my c#-application. Therefore I encountered following troubles: In my c#-app I have got a byte-array, which should somehow be...
1
by: Daniel von Fersen | last post by:
When I want to Read the Bytes 1000-2000 from a Stream into a ByteArray using Stream.Read(byteArray,1000,2000) they are written to the positions 1000-2000 in the byteArray. but my Array is...
8
by: Daniel von Fersen | last post by:
When I want to Read the Bytes 1000-2000 from a Stream into a ByteArray using Stream.Read(byteArray,1000,2000) they are written to the positions 1000-2000 in the byteArray. but my Array is...
2
by: B-Dog | last post by:
I'm trying to download a csv file into a datatable but can't seem to figure it out. I can read the csv into a bytearray and view it but I'm having trouble trying to figure out get the downloaded...
2
by: BigC | last post by:
Hi, I am having problems converting from a Structure to a ByteArray. The main problem being that the structure contains a variable length array, which cannot be defined within the structure...
1
by: gijamie911 | last post by:
int nBytes = 256; byte ByteArray = new byte; int nBytesRead = inputStream.Read(ByteArray, 0 , nBytes); recorderID = ""; for (int i...
11
by: Icemokka | last post by:
Hi, I'm need to upload a big file ( 600Mb+ ) to a BLOB field in MSSQL 2005. My code looks like this : fs = New FileStream(sFilePath, FileMode.Open) Dim ByteArray(fs.Length) As Byte...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.