2.3. Convert Basic Value Types to Byte Arrays

Problem

You need to convert basic value types to byte arrays.

Solution

The Shared methods of the System.BitConverter class provide a convenient mechanism for converting most basic value types to and from byte arrays. An exception is the Decimal type. To convert a Decimal type to or from a byte array, you need to use a System.IO.MemoryStream object.

How It Works

The Shared method GetBytes of the BitConverter class provides overloads that take most of the standard value types and return the value encoded as an array of bytes. Support is provided for the Boolean, Char, Double, Short, Integer, Long, Single, UShort, UInteger, and ULong data types. BitConverter also provides a set of Shared methods that ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.