What would be the "right" way to convert type 'byte[]' to 'string'?
Thanks
EitanB
Eitan - 05 Oct 2007 14:54 GMT
And vise vers, i.e.: convert type 'string' to 'byte[]'
> What would be the "right" way to convert type 'byte[]' to 'string'?
>
> Thanks
> EitanB
Christof Nordiek - 05 Oct 2007 15:09 GMT
> 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
Eitan - 05 Oct 2007 15:20 GMT
Thanks Christof
Eitan
> > What would be the "right" way to convert type 'byte[]' to 'string'?
> >
[quoted text clipped - 4 lines]
>
> Christof
Jon Skeet [C# MVP] - 05 Oct 2007 15:38 GMT
> 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
Eitan - 05 Oct 2007 17:31 GMT
Thanks
EitanB
> > What would be the "right" way to convert type 'byte[]' to 'string'?
>
[quoted text clipped - 6 lines]
>
> Jon
Peter Bromberg [C# MVP] - 05 Oct 2007 20:24 GMT
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);

Signature
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com
> What would be the "right" way to convert type 'byte[]' to 'string'?
>
> Thanks
> EitanB
Eitan - 08 Oct 2007 16:15 GMT
Thanks,
EitanB
> Assuming the data is in a format and encoding that will correctly convert,
> you can use shorthand static methods like:
[quoted text clipped - 7 lines]
> > Thanks
> > EitanB