473,411 Members | 1,968 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,411 software developers and data experts.

byte[] to string conversion ...

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;
}
}
Nov 23 '06 #1
5 9003
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);
}

--
Pozdrav,
Josip Medved
http://www.jmedved.com

Nov 23 '06 #2
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;
}
}
Nov 23 '06 #3
Jamie Risk <risk.#.@intectus.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 23 '06 #4
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?
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

Nov 23 '06 #5
Wiebe Tijsma <wi*********@capitalstijsma.comwrote:
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.

--
Thomas T. Veldhouse
Key Fingerprint: D281 77A5 63EE 82C5 5E68 00E4 7868 0ADC 4EFB 39F0
Nov 27 '06 #6

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

Similar topics

13
by: Bryan Parkoff | last post by:
I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T"...
43
by: Bill Cunningham | last post by:
I've been reading the C standard online and I'm puzzled as to what multibyte chars are. Wide chars I believe would be characters for languages such as cantonese or Japanese. I know the ASCII...
4
by: Dennis Myrén | last post by:
Hi. Is there a way to utilize the great primitive data type formatting routines available in .NET without working with strings? I want a byte directly rather than a string. I think it is...
3
by: pkumar | last post by:
How to convert this byte array to string byte b=new byte; Is there any function or I need read one by one and build the string thanks
25
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
11
by: chaitali.pats | last post by:
Hi , I have implemented MD5 in C language . I am getting an output of 32 bits Hexadecimal number . for example : 83a80d3ca057492f0ce99ac1db8dced0 I need to convert this string same to 16...
16
by: Hugh Janus | last post by:
Hi all, I am using the below functions in order to convert strings to bytes and vice versa. I totally ans shamefully stole these functions from this group btw! Anyway, they work great but as...
5
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.