当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET BitConverter类代码示例

本文整理汇总了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
开发者ID:VB.NET开发者,项目名称:System,代码行数:40,代码来源:BitConverter

输出:

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
开发者ID:VB.NET开发者,项目名称:System,代码行数:16,代码来源:BitConverter

输出:

-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
开发者ID:VB.NET开发者,项目名称:System,代码行数:30,代码来源:BitConverter


注:本文中的System.BitConverter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。