Tuesday, June 3, 2008

How to Convert String into Byte Array

Level: Beginner
Knowledge Required:
  • Text Encoding
  • String
  • Array
Description:
As I have discussed in my earlier post

How to Convert Byte Array into String

Now we will see how we can reverse this process i.e. Convert the String into Byte Array. We will use Text Encoding as,

byte array() = System.Text.Encoding.ASCII.GetBytes(string variable)

Usage:

Dim StringVariable As String = "ABC"
Dim ByteArray() As Byte
ByteArray = System.Text.Encoding.ASCII.GetBytes(StringVariable)
For Each b As Byte In ByteArray
Debug.WriteLine(b)
Next

Output will be:
65
66
67

See Also:

How to Convert Byte Array into String

No comments: