異世界


2012年6月23日 星期六

BitConverter

將基底資料型別與位元組陣列相互轉換

 

1. BitConverter.ToSingle 方法

public static float ToSingle (
byte[] value,
int startIndex
)
 
下列程式碼範例會以 ToSingle 方法,將 Byte 陣列的元素轉換為 Single 值。
// Example of the BitConverter.ToSingle method.
using System;
 
class BytesToSingleDemo
{
    const string formatter = "{0,5}{1,17}{2,18:E7}";
 
    // Convert four byte array elements to a float and display it.
    public static void BAToSingle( byte[ ] bytes, int index )
    {
        float value = BitConverter.ToSingle( bytes, index );
 
        Console.WriteLine( formatter, index, 
            BitConverter.ToString( bytes, index, 4 ), value );
    }
 
    // Display a byte array, using multiple lines if necessary.
    public static void WriteMultiLineByteArray( byte[ ] bytes )
    {
        const int rowSize = 20;
        int iter;
 
        Console.WriteLine( "initial byte array" );
        Console.WriteLine( "------------------" );
 
        for( iter = 0; iter < bytes.Length - rowSize; iter += rowSize )
        {
            Console.Write( 
                BitConverter.ToString( bytes, iter, rowSize ) );
            Console.WriteLine( "-" );
        }
 
        Console.WriteLine( BitConverter.ToString( bytes, iter ) );
        Console.WriteLine( );
    }
 
    public static void Main( )
    {
        byte[ ] byteArray = {
              0,   0,   0,   0, 128,  63,   0,   0, 112,  65, 
              0, 255, 127,  71,   0,   0, 128,  59,   0,   0, 
            128,  47,  73,  70, 131,   5,  75,   6, 158,  63, 
             77,   6, 158,  63,  80,   6, 158,  63,  30,  55, 
            190, 121, 255, 255, 127, 255, 255, 127, 127,   1, 
              0,   0,   0, 192, 255,   0,   0, 128, 255,   0, 
              0, 128, 127 };
 
        Console.WriteLine(
            "This example of the BitConverter.ToSingle( byte( ), " +
            "int ) \nmethod generates the following output. It " +
            "converts elements \nof a byte array to float values.\n" );
 
        WriteMultiLineByteArray( byteArray );
 
        Console.WriteLine( formatter, "index", "array elements", 
            "float" );
        Console.WriteLine( formatter, "-----", "--------------", 
            "-----" );
          
        // Convert byte array elements to float values.
        BAToSingle( byteArray, 0 );
        BAToSingle( byteArray, 2 );
        BAToSingle( byteArray, 6 );
        BAToSingle( byteArray, 10 );
        BAToSingle( byteArray, 14 );
        BAToSingle( byteArray, 18 );
        BAToSingle( byteArray, 22 );
        BAToSingle( byteArray, 26 );
        BAToSingle( byteArray, 30 );
        BAToSingle( byteArray, 34 );
        BAToSingle( byteArray, 38 );
        BAToSingle( byteArray, 42 );
        BAToSingle( byteArray, 45 );
        BAToSingle( byteArray, 49 );
        BAToSingle( byteArray, 51 );
        BAToSingle( byteArray, 55 );
        BAToSingle( byteArray, 59 );
    }
}
 
/*
This example of the BitConverter.ToSingle( byte( ), int )
method generates the following output. It converts elements
of a byte array to float values.
initial byte array
------------------
00-00-00-00-80-3F-00-00-70-41-00-FF-7F-47-00-00-80-3B-00-00-
80-2F-49-46-83-05-4B-06-9E-3F-4D-06-9E-3F-50-06-9E-3F-1E-37-
BE-79-FF-FF-7F-FF-FF-7F-7F-01-00-00-00-C0-FF-00-00-80-FF-00-
00-80-7F
index   array elements             float
-----   --------------             -----
    0      00-00-00-00    0.0000000E+000
    2      00-00-80-3F    1.0000000E+000
    6      00-00-70-41    1.5000000E+001
   10      00-FF-7F-47    6.5535000E+004
   14      00-00-80-3B    3.9062500E-003
   18      00-00-80-2F    2.3283064E-010
   22      49-46-83-05    1.2345000E-035
   26      4B-06-9E-3F    1.2345671E+000
   30      4D-06-9E-3F    1.2345673E+000
   34      50-06-9E-3F    1.2345676E+000
   38      1E-37-BE-79    1.2345679E+035
   42      FF-FF-7F-FF   -3.4028235E+038
   45      FF-FF-7F-7F    3.4028235E+038
   49      01-00-00-00    1.4012985E-045
   51      00-00-C0-FF               NaN
   55      00-00-80-FF         -Infinity
   59      00-00-80-7F          Infinity
*/

 


2. 下列程式碼範例說明數個 BitConverter 類別方法的使用方式。


// 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 ) ) );
    }
}
 
/*
This example of methods of the BitConverter class
generates the following output.
                 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
*/


 


資料來源: http://msdn.microsoft.com/zh-tw/library/3kftcaf9(v=vs.80)

沒有留言:

張貼留言