January 25, 2009

Convert Byte[] (Byte Array) to String and ViceVersa




The Encoding.GetBytes() method contains six overloads to encode all the characters into a sequence of bytes.

Convert Byte[]( Byte Array) to String

Here 'b' is the byte array

C#


    string strModified = System.Text.Encoding.Unicode.GetString(b);



VB.NET


    Dim strModified As String = System.Text.Encoding.Unicode.GetString(b)




Convert a String to Byte[] (Byte Array)

C#


    byte[] b = Encoding.Unicode.GetBytes(strOriginal);



VB.NET


    Dim b As Byte() = Encoding.Unicode.GetBytes(strOriginal)



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

2 Responses to "Convert Byte[] (Byte Array) to String and ViceVersa"
  1. John said...
    July 1, 2010 4:26 PM

    For a round trip byte[] -> string -> byte[], you are much safer using Convert.ToBase64String and FromBase64String

  2. Anonymous said...
    May 4, 2011 6:18 AM

    Nice one John!!

    thks

 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions