C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Example: The six characters are converted into bytes. In a program, these six bytes could be written to a binary file, as with BinaryWriter.
ByteCharBinaryWriterCaution: If you try to write non-ASCII characters, this will cause serious trouble. Data will be lost or even corrupted.
VB.NET program that converts String to Byte array
Module Module1
Sub Main()
' The input String.
Dim value As String = "VB.NET"
' Convert String to Byte array.
Dim array() As Byte = System.Text.Encoding.ASCII.GetBytes(value)
' Display Bytes.
For Each b As Byte In array
Console.WriteLine("{0} = {1}", b, ChrW(b))
Next
End Sub
End Module
Output
86 = V
66 = B
46 = .
78 = N
69 = E
84 = T
Output: We see that the output of this program is same as the previous program. ASCII.GetString round-trips the bytes from ASCII.GetBytes.
VB.NET program that converts Byte array to String
Module Module1
Sub Main()
' Input Bytes.
Dim array() As Byte = {86, 66, 46, 78, 69, 84}
' Use GetString.
Dim value As String = System.Text.ASCIIEncoding.ASCII.GetString(array)
Console.WriteLine(value)
End Sub
End Module
Output
VB.NET
ASCII.GetChars: This returns a Char array containing the characters represented by the individual Bytes.
String ConstructorASCII.GetCharCount: This Function returns the number of characters in the output Char array.
Array