BitConverter

BitConverter


Converts base data types to an array of bytes, and an array of bytes to base data types.


Public:

Methods:

NameDescription
 CurrencyToInt64Bits Converts the specified currency number to 64-bit signed integer.  
 DoubleToInt64Bits Converts the specified double-precision floating point number to 64-bit signed integer.  
 GetBytes Returns a byte array representation of the datatype value.  
 Int32BitsToSingle Converts the specified 32-bit signed integer to a single-precision floating point number.  
 Int64BitsToCurrency Converts the specified 64-bit signed integer to a currency number.  
 Int64BitsToDouble Converts the specified 64-bit signed integer to a double-precision floating point number.  
 SingleToInt32Bits Converts the specified single-precision floating point number to 32-bit signed integer.  
 ToBoolean Converts an array of bytes to a Boolean value.  
 ToCurrency Converts an array of bytes to a Currency value.  
 ToDate Converts an array of bytes to a Date value.  
 ToDecimal Converts an array of bytes do a Variant Decimal value.  
 ToDouble Converts an array of bytes to a Double value.  
 ToInt16 Converts an array of bytes to an Integer value.  
 ToInt32 Converts an array of bytes to a Long value.  
 ToInt64 Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array.  
 ToSingle Converts an array of bytes to a Single value.  
 ToString Converts an array of bytes to a string of hexidecimal notations.  

Remarks

The BitConverter class helps manipulate value types in their fundamental form, as a series of bytes. A byte is defined as an 8-bit unsigned integer. The BitConverter class includes static methods to convert each of the primitive types to and from an array of bytes, as the following table illustrates.
TypeTo byte conversionFrom byte conversion
BooleanGetBytes(Boolean)ToBoolean
Double GetBytes(Double)
-or-
DoubleToInt64Bits(Double)
ToDouble
-or-
Int64BitsToDouble
Int16GetBytes(Int16)ToInt16
Int32GetBytes(Int32)ToInt32
Int64GetBytes(Int64)ToInt64
SingleGetBytes(Single)ToSingle
DateGetBytes(Date)ToDate
DecimalGetBytes(Decimal)ToDecimal

Examples

The following code example illustrates the use of several BitConverter class methods.

Public Sub Main()
    Const Formatter As String = "{0,25}{1,30}"
    Const ADouble   As Double = 0.111111111111111
    Const ASingle   As Single = 0.1111111!
    Const ALong     As Long = 1111111111
    Const AnInt     As Integer = 11111
    Const ABool     As Boolean = True
    Dim AnInt64     As Int64
    
    AnInt64 = CInt64("1111111111111111111")
    
    Debug.Print t("This example of methods of the BitConverter class\ngenerates the following output.\n")
    Debug.Print CorString.Format(Formatter, "Argument", "Byte array")
    Debug.Print CorString.Format(Formatter, "--------", "----------")
    
    PrintValue ADouble
    PrintValue ASingle
    PrintValue AnInt64
    PrintValue ALong
    PrintValue AnInt
    PrintValue ABool
End Sub

Private Sub PrintValue(ByRef Value As Variant)
    Const Formatter As String = "{0,25}{1,30}"
    Dim Bytes() As Byte
    Dim Contents As String
    
    Bytes = BitConverter.GetBytes(Value)
    Contents = BitConverter.ToString(Bytes)
    
    Debug.Print CorString.Format(Formatter, Value, Contents)
End Sub

' This code produces the following output.
'
'    This example of methods of the BitConverter class
'    generates the following output.
'
'                     Argument                    Byte array
'                     --------                    ----------
'            0.111111111111111       14-C7-71-1C-C7-71-BC-3F
'                    0.1111111                   37-8E-E3-3D
'          1111111111111111111       C7-71-C4-2B-AB-75-6B-0F
'                   1111111111                   C7-35-3A-42
'                        11111                         67-2B
'                         True                            01

See Also

Project CorLib Overview

Class BitConverter Overview