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 / .NET Framework / CLR / June 2004

Tip: Looking for answers? Try searching our database.

Conversion between String and byte[]

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chuck Haeberle - 02 Jun 2004 19:01 GMT
Due to an odd conjunction of stars (trust me, thats a better reason than the real one), I need to store text data in a SQLServer Image column

I'm looking for a cleaner method to convert strings to byte[] and vice versa.  So far the only implementation I have been able to make work has been to do an iteration of the array and convert one character at a time in either direction

I've looked into Streams and Textwriters but none of them seem to be quite what I need.  Can anyone suggest a cleaner method of doing a conversion than the rather clunky
for(int i=0; i< myChars.Length; i++) { myBytes[i] = Convert.ToByte(myChars[i]);

?
Jon Skeet [C# MVP] - 02 Jun 2004 19:14 GMT
> Due to an odd conjunction of stars (trust me, thats a better reason
> than the real one), I need to store text data in a SQLServer Image
[quoted text clipped - 9 lines]
> conversion than the rather clunky: for(int i=0; i< myChars.Length;
> i++) { myBytes[i] = Convert.ToByte(myChars[i]); }

That's not just clunky - it's broken. As soon as you've got a character
with a Unicode value greater than 255, you'll get an exception.

Fortunately, the Encoding class is provided to do exactly what you
want. Just select the encoding you want and call GetBytes.

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

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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.