📜  C#| BitConverter类

📅  最后修改于: 2021-05-29 17:22:45             🧑  作者: Mango

BitConverter类的使用是将基本数据类型转换为字节数组,并将字节数组转换为基本数据类型。此类在System名称空间下定义。此类提供了不同类型的方法来执行转换。基本上,一个字节定义为8位无符号整数。此类有助于以基本形式(作为一系列字节)操纵值类型。这是在静态方法的帮助下完成的。 BitConverter类的静态方法用于将每个基本类型与字节数组之间进行转换。

场地:

  • IsLittleEndian :这指示在此计算机体系结构中存储数据的字节顺序(“字节序”)。

方法

Method Description
DoubleToInt64Bits(Double) Converts the specified double-precision floating point number to a 64-bit signed integer.
GetBytes() Converts the specified data to an array of bytes.
Int32BitsToSingle() Converts the specified 32-bit signed integer to a single-precision floating point number.
Int64BitsToDouble(Int64) Converts the specified 64-bit signed integer to a double-precision floating point number.
SingleToInt32Bits() Converts the specified single-precision floating point number to 32-bit signed integer.
ToBoolean(Byte[], Int32) Returns a Boolean value converted from the byte at a specified position in a byte array.
ToChar(Byte[], Int32) Returns a Unicode character converted from two bytes at a specified position in a byte array.
ToDouble(Byte[], Int32) Returns a double-precision floating point number converted from eight bytes at a specified position in a byte array.
ToInt16(Byte[], Int32) Returns a 16-bit signed integer converted from two bytes at a specified position in a byte array.
ToInt32(Byte[], Int32) Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array.
ToInt64(Byte[], Int32) Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array.
ToSingle(Byte[], Int32) Returns a single-precision floating point number converted from four bytes at a specified position in a byte array.
ToString() Converts the numeric value of each element of a specified array of bytes to its equivalent hexadecimal string representation.
ToUInt16(Byte[], Int32) Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array.
ToUInt32(Byte[], Int32) Returns a 32-bit unsigned integer converted from four bytes at a specified position in a byte array.
ToUInt64(Byte[], Int32) Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a byte array.

范例1:

// C# program to illustrate the
// use of GetBytes(Int64) method
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating and initializing an array
        long[] ele = {0, long.MaxValue, long.MinValue,
                         1000000000000000, -100000000, 
                       0xDDDDDDDDD, -0xAAAAAAAAAAAA };
  
        // Display the elements
        Console.WriteLine("Elements are:");
        foreach(long i in ele)
        {
            Console.WriteLine(i);
        }
  
        foreach(var num in ele)
        {
  
            // Get the specified 64-bit signed
            // integer value as an array of bytes.
            // using GetBytes(Int64) method
            byte[] BArr = BitConverter.GetBytes(num);
  
            // Display the elements
            Console.WriteLine("Element: {0}", 
                BitConverter.ToString(BArr));
        }
    }
}

输出:

Elements are:
0
9223372036854775807
-9223372036854775808
1000000000000000
-100000000
59556879837
-187649984473770
Element: 00-00-00-00-00-00-00-00
Element: FF-FF-FF-FF-FF-FF-FF-7F
Element: 00-00-00-00-00-00-00-80
Element: 00-80-C6-A4-7E-8D-03-00
Element: 00-1F-0A-FA-FF-FF-FF-FF
Element: DD-DD-DD-DD-0D-00-00-00
Element: 56-55-55-55-55-55-FF-FF

范例2:

// C# program to illustrate the
// use of Int64BitsToDouble(Int64)
// method
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        long ele = 0xFFFFFFFFFFFFFFF;
  
        // Converts the given 64-bit signed
        // integer to a double-precision 
        // floating point number using the 
        // Int64BitsToDouble(Int64) method
        double R = BitConverter.Int64BitsToDouble(ele);
  
        // Display the element
        Console.WriteLine("Converted Element is : {0}", R);
    }
}

输出:

Converted Element is : 1.28822975391943E-231

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.bitconverter?view=netframework-4.7.2