How to: Convert Strings into an Array of Bytes in Visual Basic

This topic shows how to convert a string into an array of bytes.

Example

This example uses the GetBytes method of the System.Text.Encoding.Unicode encoding class to convert a string into an array of bytes.

Private Function UnicodeStringToBytes( 
    ByVal str As String) As Byte()

    Return System.Text.Encoding.Unicode.GetBytes(str)
End Function

You can choose from several encoding options to convert a string into a byte array:

See Also

System.Text.Encoding
GetBytes
How to: Convert an Array of Bytes into a String in Visual Basic