当前位置: 首页>>代码示例>>C#>>正文


C# BitConverter类代码示例

本文整理汇总了C#中System.BitConverter的典型用法代码示例。如果您正苦于以下问题:C# BitConverter类的具体用法?C# BitConverter怎么用?C# BitConverter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


BitConverter类属于System命名空间,在下文中一共展示了BitConverter类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

// Example of BitConverter class methods.
using System;

class BitConverterDemo
{
    public static void Main( )
    {
        const string formatter = "{0,25}{1,30}";
 
        double  aDoubl  = 0.1111111111111111111;
        float   aSingl  = 0.1111111111111111111F;
        long    aLong   = 1111111111111111111;
        int     anInt   = 1111111111;
        short   aShort  = 11111;
        char    aChar   = '*';
        bool    aBool   = true;

        Console.WriteLine( 
            "This example of methods of the BitConverter class" +
            "\ngenerates the following output.\n" );
        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 ) ) );
    }
}
开发者ID:.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: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      int value = -16;
      Byte[] bytes = BitConverter.GetBytes(value); 
      
      // Convert bytes back to Int32.
      int intValue = BitConverter.ToInt32(bytes, 0);
      Console.WriteLine("{0} = {1}: {2}", 
                        value, intValue, 
                        value.Equals(intValue) ? "Round-trips" : "Does not round-trip");
      // Convert bytes to UInt32.
      uint uintValue = BitConverter.ToUInt32(bytes, 0);
      Console.WriteLine("{0} = {1}: {2}", value, uintValue, 
                        value.Equals(uintValue) ? "Round-trips" : "Does not round-trip");
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:21,代码来源:BitConverter

输出:

-16 = -16: Round-trips
-16 = 4294967280: Does not round-trip

示例3: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      int value = 12345678;
      byte[] bytes = BitConverter.GetBytes(value);
      Console.WriteLine(BitConverter.ToString(bytes));
      
      if (BitConverter.IsLittleEndian)
         Array.Reverse(bytes); 

      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)
         Array.Reverse(bytes);

      Console.WriteLine(BitConverter.ToString(bytes));
      int result = BitConverter.ToInt32(bytes, 0);
      Console.WriteLine("Original value: {0}", value);
      Console.WriteLine("Returned value: {0}", result);
   }
}
// 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:.NET开发者,项目名称:System,代码行数:35,代码来源:BitConverter


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