convert type 'byte[]' to 'string'

  • Thread starter Thread starter Guest
  • Start date Start date
Eitan said:
What would be the "right" way to convert type 'byte[]' to 'string'?
The method GetString of the right Encoding instance.
For the other direction use GetBytes.

In either case, you have to know the right encoding

Christof
 
Thanks Christof
Eitan

Christof Nordiek said:
Eitan said:
What would be the "right" way to convert type 'byte[]' to 'string'?
The method GetString of the right Encoding instance.
For the other direction use GetBytes.

In either case, you have to know the right encoding

Christof
 
What would be the "right" way to convert type 'byte[]' to 'string'?

That entirely depends on what the data is. If it's intrinsically text
data, you should pick the right encoding and use Encoding.GetString.

If it's arbitrary binary data that you want to preserve as a string
and then convert back to binary data later, use Convert.ToBase64String
and Convert.FromBase64String.

Jon
 
Thanks
EitanB

Jon Skeet said:
What would be the "right" way to convert type 'byte[]' to 'string'?

That entirely depends on what the data is. If it's intrinsically text
data, you should pick the right encoding and use Encoding.GetString.

If it's arbitrary binary data that you want to preserve as a string
and then convert back to binary data later, use Convert.ToBase64String
and Convert.FromBase64String.

Jon
 
Assuming the data is in a format and encoding that will correctly convert,
you can use shorthand static methods like:

string myString =System.Text.Encoding.UTF8.GetString(myByteArray);
and
byte[] myByteArray = System.Text.Encoding.UTF8.GetBytes(myString);
 
Thanks,
EitanB

Peter Bromberg said:
Assuming the data is in a format and encoding that will correctly convert,
you can use shorthand static methods like:

string myString =System.Text.Encoding.UTF8.GetString(myByteArray);
and
byte[] myByteArray = System.Text.Encoding.UTF8.GetBytes(myString);
--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com



Eitan said:
What would be the "right" way to convert type 'byte[]' to 'string'?

Thanks
EitanB
 

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

Back
Top