System.BitConverter 类

方法描述

将基础数据类型与字节数组相互转换。

语法定义(C# System.BitConverter 类 的用法)

public static class BitConverter

构造函数

构造函数名称 构造函数描述

成员/方法

方法名称 方法描述
DoubleToInt64Bits 将指定的双精度浮点数转换为 64 位有符号整数。
GetBytes(Boolean) 以字节数组的形式返回指定的布尔值。
GetBytes(Char) 以字节数组的形式返回指定的 Unicode 字符值。
GetBytes(Double) 以字节数组的形式返回指定的双精度浮点值。
GetBytes(Int16) 以字节数组的形式返回指定的 16 位有符号整数值。
GetBytes(Int32) 以字节数组的形式返回指定的 32 位有符号整数值。
GetBytes(Int64) 以字节数组的形式返回指定的 64 位有符号整数值。
GetBytes(Single) 以字节数组的形式返回指定的单精度浮点值。
GetBytes(UInt16) 以字节数组的形式返回指定的 16 位无符号整数值。
GetBytes(UInt32) 以字节数组的形式返回指定的 32 位无符号整数值。
GetBytes(UInt64) 以字节数组的形式返回指定的 64 位无符号整数值。
Int64BitsToDouble 将指定的 64 位有符号整数转换成双精度浮点数。
ToBoolean 返回由字节数组中指定位置的一个字节转换来的布尔值。
ToChar 返回由字节数组中指定位置的两个字节转换来的 Unicode 字符。
ToDouble 返回由字节数组中指定位置的八个字节转换来的双精度浮点数。
ToInt16 返回由字节数组中指定位置的两个字节转换来的 16 位有符号整数。
ToInt32 返回由字节数组中指定位置的四个字节转换来的 32 位有符号整数。
ToInt64 返回由字节数组中指定位置的八个字节转换来的 64 位有符号整数。
ToSingle 返回由字节数组中指定位置的四个字节转换来的单精度浮点数。
ToString(Byte[]) 将指定的字节数组的每个元素的数值转换为它的等效十六进制字符串表示形式。
ToString(Byte[], Int32) 将指定的字节子数组的每个元素的数值转换为它的等效十六进制字符串表示形式。
ToString(Byte[], Int32, Int32) 将指定的字节子数组的每个元素的数值转换为它的等效十六进制字符串表示形式。
ToUInt16 返回由字节数组中指定位置的两个字节转换来的 16 位无符号整数。
ToUInt32 返回由字节数组中指定位置的四个字节转换来的 32 位无符号整数。
ToUInt64 返回由字节数组中指定位置的八个字节转换来的 64 位无符号整数。

提示和注释

BitConverter 类以一系列字节的形式有助于操控基本窗体中的值类型。 一个字节定义为一个 8 位无符号整数。 如下表所示,BitConverter 类包括用静态方法将每个基元类型转换成字节数组和将字节数组转换成其基元类型。

Type

转换为字节

从字节转换

Boolean

GetBytes(Boolean)

ToBoolean

Char

GetBytes(Char)

ToChar

Double

GetBytes(Double)

- 或 -

DoubleToInt64Bits(Double)

ToDouble

- 或 -

Int64BitsToDouble

Int16

GetBytes(Int16)

ToInt16

Int32

GetBytes(Int32)

ToInt32

Int64

GetBytes(Int64)

ToInt64

Single

GetBytes(Single)

ToSingle

UInt16

GetBytes(UInt16)

ToUInt16

UInt32

GetBytes(UInt32)

ToUInt32

UInt64

GetBytes(UInt64)

ToUInt64

如果您使用 BitConverter 方法往返数据,请确保 GetBytes 重载与 To类型 方法指定相同的类型。 下面的示例所示,还原一个数组,表示有符号的整数,通过调用 ToUInt32 可能至使出现一个与原值不同的数值。 有关更多信息,请参见 BCL 团队博客上的文章 Working with Signed Non-Decimal and Bitwise Values(使用有符号非小数和按位值)。

C#

VB

复制

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");

}

}

// The example displays the following output:

// -16 = -16: Round-trips

// -16 = 4294967280: Does not round-trip

由 GetBytes 方法重载返回的数组字节顺序(以及 DoubleToInt64Bits 方法返回整数的位顺序和 ToString(Byte[]) 方法返回的十六进制字符串顺序)取决于计算机结构是小字节序,还是大字节序。 由 ToIntegerValue 方法和 ToChar 重载返回的数组字节顺序(取决于计算机结构是小字节序,还是大字节序)。 结构的 Endian 设置是由 IsLittleEndian 属性指示,它在小字节序系统返回 true,在大字节序系统返回 false。 在小字节序系统中,较低序位字节先于更高序位字节。 在大字节序系统中,更高序位字节先于较低序位字节。 下表说明了将整数 1,234,567,890 (0x499602D2) 传递到 GetBytes(Int32) 方法所产生的字节数组方面的区别。 这些字节是按字节从索引 0 到索引 3 的顺序依次排列的。

Little-endian

D2-02-96-49

Big-endian

49-96-02-D2

因为某些方法的返回值取决于系统体系结构,传输字节数据到计算机边界之外时,一定要小心:

如果保证发送和接收数据的所有系统都具有相同的字节排序方式,不会执行任何操作完成的数据。

如果发送和接收数据的系统可以有不同字节排序方式,请始终以特定的顺序传输数据。 这意味着在发送它们之前或接收之后数组中的字节的顺序可能必须倒过来。 通用约定是以网络字节顺序(大字节序顺序)传输数据。 下面的示例为以网络字节顺序发送一个整数值提供了实现。

C#

VB

复制

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)

bytes = ReverseBytes(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)

bytes = ReverseBytes(bytes);

Console.WriteLine(BitConverter.ToString(bytes));

int result = BitConverter.ToInt32(bytes, 0);

Console.WriteLine("Original value: {0}", value);

Console.WriteLine("Returned value: {0}", result);

}

private static byte[] ReverseBytes(byte[] inArray)

{

byte temp;

int highCtr = inArray.Length - 1;

for (int ctr = 0; ctr < inArray.Length / 2; ctr++)

{

temp = inArray[ctr];

inArray[ctr] = inArray[highCtr];

inArray[highCtr] = temp;

highCtr -= 1;

}

return inArray;

}

}

// The example displays the following output on a little-endian system:

// 4E-61-BC-00

// 00-61-BC-4E

// 00-61-BC-4E

// 4E-61-BC-00

// Original value: 12345678

// Returned value: 12345678

如果发送和接收数据的系统可以有不同字节排序方式,并且传输的数据包含有符号整数,调用 IPAddress.HostToNetworkOrder 方法以将数据转换成网络字节顺序,以及调用 IPAddress.NetworkToHostOrder 方法以将其转换为收件人所需的顺序。

System.BitConverter 类例子

下面的代码示例演示多个 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
*/

继承层次结构

System.Object

System.BitConverter

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。