Convert a byte array to a string with vb.net

Source: Internet
Author: User
Tags base64
If you need to convert a byte array to a string, you can use the Bitconverter.tostring or Covert.tobase64string method. Below I provide an example of how to use the previous two methods for conversion.

byte array usage

If you are ready to restore 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 quickest way to convert a byte array to a string. This class provides a method for converting between the base data type and the byte array. To use this method, use an overloaded ToString method to accept a byte array as a parameter. In this case, the string includes each hexadecimal-formatted value of the byte array, separated by a dash. There is no way to use strings to undo the conversion automatically and restore the original byte array. See the example in List A.

Listing A

Private Sub convertbytearraytostring ()

Dim Bttext () as Byte = {0, 100, 200, 255, 5}

Dim StrText as String

StrText = bitconverter.tostring (Bttext)
MessageBox.Show (StrText)

End Sub


By using the ToBase64String and Frombase64stirng methods of the System.Convert class, the BASE64 encoding can also be used to convert byte arrays into strings. In BASE64 encoding, each three-byte sequence is converted to four bytes. Each BASE64 encoded character is one of 64 possible values in the domain. See the example in List B.

Listing B

Private Sub convertbytearraytostring ()

Dim Bttext () as Byte = {0, 100, 200, 255, 5}

Dim StrText as String

StrText = convert.tobase64string (Bttext)
MessageBox.Show (StrText)

End Sub


Both of these methods can be used to establish a binary data representation. Remember, in order to restore a byte array to real text information, you must use the correct encoding class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.