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, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) 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 is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 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