Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

convert type 'byte[]' to 'string'

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eitan - 05 Oct 2007 14:51 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.