Re: byte[] to string conversion ...
- From: Wiebe Tijsma <wiebeREMOVE@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 23 Nov 2006 20:54:19 +0100
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
Jamie Risk wrote:
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;
}
}
- References:
- byte[] to string conversion ...
- From: Jamie Risk
- byte[] to string conversion ...
- Prev by Date: byte[] to string conversion ...
- Next by Date: Re: byte[] to string conversion ...
- Previous by thread: byte[] to string conversion ...
- Next by thread: Re: byte[] to string conversion ...
- Index(es):
Relevant Pages
|