Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Convert a byte array into a string with VB.NET.
Convert a byte array into a string with VB.NET.
Recently, the method of converting a string into a byte array with VB NET was explained. If you need to convert a byte array into a string, you can use the BitConverter ToString or Covert ToBase String method. Below, I will illustrate how to use the above two methods for conversion.

Byte array usage

If you want to recover the text saved in binary format, call the GetString method of the appropriately encoded object in the system text namespace.

Using the System BitConverter class is the fastest way to convert a byte array into a string. This class provides a method for conversion between basic data types and byte arrays. To use this method, you can use the overloaded ToString method to accept an array of bytes as a parameter. In this case, the string includes each hexadecimal value of the byte array, which is separated by a dash. Using strings cannot automatically undo the conversion and restore the original byte array. See the example in listing a.

Listing a

? private Sub ConvertByteArrayToString()? Dim btText() As Byte = { }? Dim strText as a string? strText = bit converter ToString(Bt text)? MessageBox Show(strText)? End Sub can also convert byte arrays into strings by using the ToBase String and frombasestiring methods of the System Convert class. In basic coding, each three-byte sequence is converted into four bytes, and each basic coding character is a possible value within this range. See the example in listing b.

Listing b

? private Sub ConvertByteArrayToString()? Dim btText() As Byte = { }? Dim strText as a string? StrText = Convert to Basic String (btText)? MessageBox Show(strText)? Both of the above methods can be used to establish binary data representation. Remember, in order to restore a byte array to true text information, you must use the correct encoding class.

Lishi Xinzhi/Article/program/net/20 13 1 1/ 1 1708