本文整理匯總了VB.NET中System.BitConverter類的典型用法代碼示例。如果您正苦於以下問題:VB.NET BitConverter類的具體用法?VB.NET BitConverter怎麽用?VB.NET BitConverter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了BitConverter類的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: BitConverterDemo
' Example of BitConverter class methods.
Module BitConverterDemo
Sub Main( )
Const formatter As String = "{0,25}{1,30}"
Dim aDoubl As Double = 0.1111111111111111111
Dim aSingl As Single = 0.1111111111111111111
Dim aLong As Long = 1111111111111111111
Dim anInt As Integer = 1111111111
Dim aShort As Short = 11111
Dim aChar As Char = "*"c
Dim aBool As Boolean = True
Console.WriteLine( _
"This example of methods of the BitConverter class" & _
vbCrLf & "generates the following output." & vbCrLf )
Console.WriteLine( formatter, "argument", "Byte array" )
Console.WriteLine( formatter, "--------", "----------" )
' Convert values to Byte arrays and display them.
Console.WriteLine( formatter, aDoubl, _
BitConverter.ToString( BitConverter.GetBytes( aDoubl ) ) )
Console.WriteLine( formatter, aSingl, _
BitConverter.ToString( BitConverter.GetBytes( aSingl ) ) )
Console.WriteLine( formatter, aLong, _
BitConverter.ToString( BitConverter.GetBytes( aLong ) ) )
Console.WriteLine( formatter, anInt, _
BitConverter.ToString( BitConverter.GetBytes( anInt ) ) )
Console.WriteLine( formatter, aShort, _
BitConverter.ToString( BitConverter.GetBytes( aShort ) ) )
Console.WriteLine( formatter, aChar, _
BitConverter.ToString( BitConverter.GetBytes( aChar ) ) )
Console.WriteLine( formatter, aBool, _
BitConverter.ToString( BitConverter.GetBytes( aBool ) ) )
End Sub
End Module
' This example of methods of the BitConverter class
輸出:
argument Byte array -------- ---------- 0.111111111111111 1C-C7-71-1C-C7-71-BC-3F 0.1111111 39-8E-E3-3D 1111111111111111111 C7-71-C4-2B-AB-75-6B-0F 1111111111 C7-35-3A-42 11111 67-2B * 2A-00 True 01
示例2: Example
Module Example
Public Sub Main()
Dim value As Integer = -16
Dim bytes() As Byte = BitConverter.GetBytes(value)
' Convert bytes back to Int32.
Dim intValue As Integer = BitConverter.ToInt32(bytes, 0)
Console.WriteLine("{0} = {1}: {2}",
value, intValue,
If(value.Equals(intValue), "Round-trips", "Does not round-trip"))
' Convert bytes to UInt32.
Dim uintValue As UInteger = BitConverter.ToUInt32(bytes, 0)
Console.WriteLine("{0} = {1}: {2}", value, uintValue,
If(value.Equals(uintValue), "Round-trips", "Does not round-trip"))
End Sub
End Module
輸出:
-16 = -16: Round-trips -16 = 4294967280: Does not round-trip
示例3: Example
Module Example
Public Sub Main()
Dim value As Integer = 12345678
Dim bytes() As Byte = BitConverter.GetBytes(value)
Console.WriteLine(BitConverter.ToString(bytes))
If BitConverter.IsLittleEndian Then
Array.Reverse(bytes)
End If
Console.WriteLine(BitConverter.ToString(bytes))
' Call method to send byte stream across machine boundaries.
' Receive byte stream from beyond machine boundaries.
Console.WriteLine(BitConverter.ToString(bytes))
If BitConverter.IsLittleEndian Then
Array.Reverse(bytes)
End If
Console.WriteLine(BitConverter.ToString(bytes))
Dim result As Integer = BitConverter.ToInt32(bytes, 0)
Console.WriteLine("Original value: {0}", value)
Console.WriteLine("Returned value: {0}", result)
End Sub
End Module
' The example displays the following output on a little-endian system:
' 4E-61-BC-00
' 00-BC-61-4E
' 00-BC-61-4E
' 4E-61-BC-00
' Original value: 12345678
' Returned value: 12345678