TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Convert String, Byte Array

Use System.Text.Encoding to convert a String into a Byte array. Then reverse the conversion.
Convert String, Byte Array. A String can be stored as a series of Bytes. This is an efficient encoding for ASCII-only Strings. It can make file formats more efficient. In VB.NET we can convert Strings to Byte arrays.
Example. The .NET Framework provides the powerful ASCII.GetBytes() Function. This Function receives an argument of String type. And it returns a Byte array reference. We do not need to allocate the Byte array—this is automatically done.Byte Array

Example: The six characters are converted into bytes. In a program, these six bytes could be written to a binary file, as with BinaryWriter.

ByteCharBinaryWriter

Caution: 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
Example 2. Continuing on, it is possible to convert Bytes into Strings. We do this with the ASCII.GetString() Function. This Function receives a Byte array argument. It returns a String instance containing the characters represented by those bytes.

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
Notes, char functions. Other Functions, such as ASCII.GetChars, are also available. We can get char arrays, or counts of chars, from these functions. These are less often needed in my experience.

ASCII.GetChars: This returns a Char array containing the characters represented by the individual Bytes.

String Constructor

ASCII.GetCharCount: This Function returns the number of characters in the output Char array.

Array
Summary. Developing an efficient way to store character data is important. It helps programs save data faster. It helps them load data faster. It also creates a file format standard that, if enforced, improves program quality.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf