By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,435 Members | 2,939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,435 IT Pros & Developers. It's quick & easy.

converting array of byte data type to string.

P: n/a
Hi all,

I am a Beginner to c#.
I Need to convert array of byte data type to string. How to do this..

ex:

byte[] bytes = new byte[1000];

this bytes get filled up. Now I need to convert to string.
How to do this..

Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

Why not use this function:

public string writeByteArrayToString(byte[] byteArray)
{
ASCIIEncoding ascii = new ASCIIEncoding();
char[] charArray = ascii.GetChars(byteArray);
return new string(charArray);
}

Don't forget to include System.Text

Cheers,
Christiaan

<sh*********@techie.com> schreef in bericht
news:11*********************@g49g2000cwa.googlegro ups.com...
Hi all,

I am a Beginner to c#.
I Need to convert array of byte data type to string. How to do this..

ex:

byte[] bytes = new byte[1000];

this bytes get filled up. Now I need to convert to string.
How to do this..

Nov 17 '05 #2

P: n/a
Hi,
string str = System.Text.Encoding.ASCII.GetString (bytes);

Ab.
http://joehacker.blogspot.com
<sh*********@techie.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hi all,

I am a Beginner to c#.
I Need to convert array of byte data type to string. How to do this..

ex:

byte[] bytes = new byte[1000];

this bytes get filled up. Now I need to convert to string.
How to do this..

Nov 17 '05 #3

P: n/a
Hi,

You need to know the format the bytes are in, are they ASCII, UTF8 or what?

Then you use the correct Encoding class.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<sh*********@techie.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hi all,

I am a Beginner to c#.
I Need to convert array of byte data type to string. How to do this..

ex:

byte[] bytes = new byte[1000];

this bytes get filled up. Now I need to convert to string.
How to do this..

Nov 17 '05 #4

P: n/a
Christiaan van Bergen <cv********@bergler.nl> wrote:
Why not use this function:

public string writeByteArrayToString(byte[] byteArray)
{
ASCIIEncoding ascii = new ASCIIEncoding();
char[] charArray = ascii.GetChars(byteArray);
return new string(charArray);
}

Don't forget to include System.Text


Reasons not to use this function:

1) Its name doesn't follow the .NET naming conventions
2) It creates a new instance of ASCIIEncoding for no reason
3) It creates a new char array for no reason
4) It's less clear than Encoding.ASCII.GetString(whatever);

You did ask ;)

--
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 17 '05 #5

P: n/a
<sh*********@techie.com> wrote:
Hi all,

I am a Beginner to c#.
I Need to convert array of byte data type to string. How to do this..

ex:

byte[] bytes = new byte[1000];

this bytes get filled up. Now I need to convert to string.
How to do this..


You need to choose the right encoding, and then ask it to decode your
bytes to a string. Unless your byte array is absolutely full of useful
data, you should specify how much of it to decode.

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

--
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 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.