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 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

Tasks

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

Reference

System.Text.Encoding

GetBytes