Software Development

Convert a string into a byte array in VB.NET


If you need to convert a string into a byte array in VB.NET, you can use the GetBytes method of one of the encoding objects available in the System.Text namespace. This tip shows various encoding types and looks at ways to convert a string into a byte array.

Encoding types

There are a number of ways to represent a string variable in a binary form. The encoding object you utilize to achieve this functionality depends on the encoding you selected.

Here are the encoding types that developers use most often:

  • ASCII: Encodes each character in a string using seven bits. This encoding type cannot contain extended Unicode characters.
  • Full Unicode (UTF-16): Represents each character in a string using 16 bits. This results in a byte array that has two bytes for each character.
  • UTF-7: Uses seven bits for ordinary ASCII characters and multiple seven-bit pairs for extended characters. This encoding type is most often used with seven-bit protocols, such as mail.
  • UTF-8: Uses eight bits for ordinary ASCII characters and multiple eight-bit pairs for extended characters. This results in a byte array that has one byte for each character (assuming there are no extended characters).
.NET offers a class for each type of encoding in the System.Text namespace. To perform encoding of a string into a byte array, you need to use an appropriate encoding object and then call its GetBytes method. See the example in Listing A. You can also access a pre-instantiated encoding object through shared properties of the base System.Text.Encoding.UTF8 class. See the example in Listing B. Note: In .NET, UTF-8 is the preferred standard for encoding since it supports the full range of Unicode characters. Also, it uses an adaptive format that results in a reduced size of the binary data if you are not using extended characters. When encoding ordinary ASCII characters, UTF-8 encoding and ASCII encoding provide the same result. In addition, by default, .NET classes such as StreamReader and StreamWriter use UTF-8 encoding when reading or writing from a stream.

Irina Medvinskaya has been involved in technology since 1996. She has an MBA from Pace University and works as a project manager at Citigroup.

----------------------------------------------------------------------------------------

Get Visual Basic tips in your inbox

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically subscribe today!

6 comments
collinsauve
collinsauve

post deleted. i think i misinterpretted what you were trying to say

aikimark
aikimark

Of course, you could just the StrConv() function that is part of the Microsoft.VisualBasic namespace. This way there is no need to involve another (system.text) namespace and your VB classic code still works.

benbodei
benbodei

In other programming languages like Dbase and Basic, one can use Substr(Sentence, 1,4) to read the first four characters of the sentence. In Basic Language we use Left$, Mid$ or Right$ to select some characters of a sentence. How is string manioulation of this type carried out in Visual Basic

collinsauve
collinsauve

anything in the Microsoft.VisualBasic is unmanaged code, and should be avoided whenever possible.

Tony Hopkinson
Tony Hopkinson

SubString, for Left and Mid. Right you would have to mess about a bit. In .NET you have to be a bit more careful with strings if you want to change them about Look Up the StringBuilder Class, there's loads in there.