VB.NET || How To Convert A String To Byte Array & Byte Array To String Using VB.NET
The following is a module with functions which demonstrates how to convert a string to a byte array and a byte array to a string using VB.NET.
1. String To Byte Array
The example below demonstrates the use of ‘Utils.GetBytes‘ to convert a string to a byte array. The optional parameter allows to specify the encoding.
1 2 3 4 5 6 7 8 |
' String To Byte Array Imports Utils Dim text = $"My Programming Notes!" Dim bytes = text.GetBytes() ' Do something with the byte array |
2. Byte Array To String
The example below demonstrates the use of ‘Utils.GetString‘ to convert a byte array to a string. The optional parameter allows to specify the encoding.
1 2 3 4 5 6 7 8 9 |
' Byte Array To String Imports Utils Dim text = $"My Programming Notes!" Dim bytes = text.GetBytes() Dim str = bytes.GetString() ' Do something with the string |
3. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 5, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils ''' <summary> ''' Decodes all bytes in the specified byte array into a string ''' </summary> ''' <param name="bytes">The byte array containing the sequence of bytes to decode</param> ''' <param name="encode">The type of encoding to be used. Default is UTF8Encoding</param> ''' <returns> A byte array containing the results of encoding</returns> <Runtime.CompilerServices.Extension()> Public Function GetString(bytes As Byte(), Optional encode As System.Text.Encoding = Nothing) As String If encode Is Nothing Then encode = New System.Text.UTF8Encoding End If Return encode.GetString(bytes) End Function ''' <summary> ''' Encodes the characters in the specified string into a sequence of bytes ''' </summary> ''' <param name="str">The string containing the characters to encode</param> ''' <param name="encode">The type of encoding to be used. Default is UTF8Encoding</param> ''' <returns> A byte array containing the results of encoding</returns> <Runtime.CompilerServices.Extension()> Public Function GetBytes(str As String, Optional encode As System.Text.Encoding = Nothing) As Byte() If encode Is Nothing Then encode = New System.Text.UTF8Encoding End If Return encode.GetBytes(str) End Function End Module End Namespace ' http://programmingnotes.org/ |
4. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 5, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Module Program Sub Main(args As String()) Try Dim text = $"My Programming Notes!" Dim bytes = Utils.GetBytes(text) Dim str = Utils.GetString(bytes) Display(str) Display($"{str = text}") Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply