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)



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

2 comments:

John said...

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

Anonymous said...

Nice one John!!

thks