當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。