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)

2 comments:

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

    ReplyDelete
  2. Nice one John!!

    thks

    ReplyDelete