Convert string to byte array and back

By Allen Stoner

Many function and procedures in use byte arrays as parameter, but few people are able to easily generate byte arrays, let alone remember what the byte array is. Here are some simple functions to convert a string to a byte array and to convert a byte array back to a string.

  Public Function StrToByteArray(ByVal str As String) As Byte()
    Dim encoding As New System.Text.UTF8Encoding()
    Return encoding.GetBytes(str)
  End Function 'StrToByteArray

  Public Function ByteArrayToStr(ByVal byteArray As Byte()) As String
    Dim str As String
    Dim enc As New System.Text.UTF8Encoding()
    str = enc.GetString(byteArray)
    Return str
  End Function

Convert string to byte array and back  (2582 Views)