byte array to string

R

realgeek

I have a byte array with binary data and I want to get its contants
into a string.


tagContent = System.Text.Encoding.ASCII.GetString(str);

screws binary data.


foreach (byte b in str)
{
tagContent += (char)b;
}

is damn slow.


What's the correct way to do this?
 
J

Jon Skeet [C# MVP]

I have a byte array with binary data and I want to get its contants
into a string.

tagContent = System.Text.Encoding.ASCII.GetString(str);

screws binary data.

Indeed it would.
foreach (byte b in str)
{
tagContent += (char)b;
}

is damn slow.

Absolutely. Using a StringBuilder would improve things, but you've
still got the same fundamental problem (see below), and there's no
guarantee that the characters you get out will be correctly transmitted
over whatever medium you're using.
What's the correct way to do this?

The correct way is not to try to mix binary and text data to start
with. Don't try to treat binary data as if it's character data, or even
as if it's encoded character data in a particular encoding.

Instead, use Base64 - Convert.ToBase64String and
Convert.FromBase64String are good ways of converting from binary data
to text data. They only use ASCII characters, so they're likely to get
transmitted correctly over whatever you want to use.
 
R

realgeek

Problem is, the initial setup was thought of as "having string data"
all the time, then the possibility of having binary data came up so i
threw in a quick fix for the method initially accepting a string
parameter, convert binary data to string without .Encoding class, and
reconvert back in the method. Unfortunately it happedned to be a *slow*
fix :)

Ok, a question, is there any easy way to distinguish betwen ASCII
encoded text and binary data given a byte array?
 
J

Jon Skeet [C# MVP]

Problem is, the initial setup was thought of as "having string data"
all the time, then the possibility of having binary data came up so i
threw in a quick fix for the method initially accepting a string
parameter, convert binary data to string without .Encoding class, and
reconvert back in the method. Unfortunately it happedned to be a *slow*
fix :)

Ok, a question, is there any easy way to distinguish betwen ASCII
encoded text and binary data given a byte array?

Well, if any of the bytes are > 127, it's certainly not ASCII encoded
string. Other than that, you can't tell - because any ASCII encoded
string *might* be binary data instead.
 
R

realgeek

Oh and plus checking for >127 bytes will be about as long as per-byte
conversion is :(
 
J

Jon Skeet [C# MVP]

By ASCII encoded string I mean "won't be screwed up by
Encoding.ASCII.GetString"
:)

It would be much, much better not to try in the first place. Keep text
as text and binary as binary. When you need to write binary, don't try
to convert it into a string first. When you need to write text, make
sure you're using the appropriate encoding. Following those simple
rules solves an awful lot of problems similar to this one.
 

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