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 EncodingUnicode 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:
EncodingASCII: Gets an encoding for the ASCII (7-bit) character set.
EncodingBigEndianUnicode: Gets an encoding for the UTF-16 format using the big-endian byte order.
EncodingDefault: Gets an encoding for the system's current ANSI code page.
EncodingUnicode: Gets an encoding for the UTF-16 format using the little-endian byte order.
EncodingUTF32: Gets an encoding for the UTF-32 format using the little-endian byte order.
EncodingUTF7: Gets an encoding for the UTF-7 format.
EncodingUTF8: Gets an encoding for the UTF-8 format.
See Also
Tasks
How to: Convert an Array of Bytes into a String in Visual Basic