byte[] to string conversion ...

J

Jamie Risk

This is the code snippet that I've come up to convert a byte[]
to string. Is there a best practiced method for such a conversion?

- Jamie

public static string ByteArrayToString(byte[] array)
{
if (null == array || 0 == array.Length)
{
throw new NullReferenceException();
return null;
}
else
{
Encoding ascii = Encoding.ASCII;
char[] asciiChars =
new char[ascii.GetCharCount(array, 0, array.Length)];
string str = null;

ascii.GetChars(array, 0, array.Length, asciiChars, 0);

foreach (string s in (new string(asciiChars)).Split('\n'))
{
if (str != null) str += Environment.NewLine;
str += s.Trim();
}
return str;
}
}
 
W

Wiebe Tijsma

Hi Jamie,

System.Encoding has already the appropriate methods:

public static string ByteArrayToString(byte[] array)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
return Encoding.ASCII.GetString(array);
}

public static byte[] StringToByteArray(string value)
{
if (value == null)
{
throw new ArgumentNullException("array");
}
return Encoding.ASCII.GetBytes(value);
}

Best Regards,

Wiebe Tijsma
http://www.e-office.com
 
J

Jon Skeet [C# MVP]

Jamie Risk said:
This is the code snippet that I've come up to convert a byte[]
to string. Is there a best practiced method for such a conversion?

Well, for a start the easiest way of doing the first part of your work
is to call Encoding.ASCII.GetString(array) instead of going through
GetCharCount, GetChars and new string(...).

Now, it looks like the rest of your conversion is to trim each line,
and then glue them back together using Environment.NewLine - correct?

If so, I'd use String.Split to get the array, then call Trim on each
element, then call String.Join to join them back together again
afterwards.

By the way, I don't *believe* String.Split will ever return a null
element.
 
J

Josip Medved

This is the code snippet that I've come up to convert a byte[]
to string. Is there a best practiced method for such a conversion?

public static string ByteArrayToString(byte[] array)
return System.Text.ASCIIEncoding.ASCII.GetString(array);
}
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Jamie said:
This is the code snippet that I've come up to convert a byte[] to
string. Is there a best practiced method for such a conversion?
public static string ByteArrayToString(byte[] array)
{
if (null == array || 0 == array.Length)
{
throw new NullReferenceException();
return null;
}
else
{
Encoding ascii = Encoding.ASCII;
char[] asciiChars =
new char[ascii.GetCharCount(array, 0, array.Length)];
string str = null;

ascii.GetChars(array, 0, array.Length, asciiChars, 0);

foreach (string s in (new string(asciiChars)).Split('\n'))
{
if (str != null) str += Environment.NewLine;
str += s.Trim();
}
return str;
}
}

What does the code do that:

ascii.GetString(b)

or maybe

ascii.GetString(b).Replace(@"\n", Environment.NewLine)

does not ?

I guess it trim trailing space of lines, but is that really
needed.

If you need the trimming then maybe a simple for loop
copying from one byte array to another and a ascii.GetString
on the new array.

Arne
 
T

Thomas T. Veldhouse

Wiebe Tijsma said:
Hi Jamie,

System.Encoding has already the appropriate methods:

public static string ByteArrayToString(byte[] array)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
return Encoding.ASCII.GetString(array);
}

"zeros" will get you in trouble. Base64 is probably what you are looking for
if you intend to transfer a byte array as a string.
 

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