BitConverter 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将基数据类型转换为一个字节数组以及将一个字节数组转换为基数据类型。
public ref class BitConverter abstract sealed
public ref class BitConverter sealed
public static class BitConverter
public sealed class BitConverter
type BitConverter = class
Public Class BitConverter
Public NotInheritable Class BitConverter
- 继承
-
BitConverter
示例
下面的代码示例演示了如何使用多个 BitConverter 类方法。
// Example of BitConverter class methods.
using namespace System;
int main()
{
String^ formatter = "{0,25}{1,30}";
double aDoubl = 0.1111111111111111111;
float aSingl = 0.1111111111111111111F;
__int64 aLong = 1111111111111111111;
int anInt = 1111111111;
short aShort = 11111;
__wchar_t aChar = L'*';
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
*/
// 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
*/
open System
let print: obj -> obj -> unit = printfn "%25O%30O"
let aDoubl = 0.1111111111111111111
let aSingl = 0.1111111111111111111f
let aLong = 1111111111111111111L
let anInt = 1111111111
let aShort = 11111s
let aChar = '*'
let aBool = true
printfn "This example of methods of the BitConverter class\ngenerates the following output.\n"
print "argument" "byte array"
print "--------" "----------"
// Convert values to Byte arrays and display them.
print aDoubl (BitConverter.ToString(BitConverter.GetBytes aDoubl))
print aSingl (BitConverter.ToString(BitConverter.GetBytes aSingl))
print aLong (BitConverter.ToString(BitConverter.GetBytes aLong))
print anInt (BitConverter.ToString(BitConverter.GetBytes anInt))
print aShort (BitConverter.ToString(BitConverter.GetBytes aShort))
print aChar (BitConverter.ToString(BitConverter.GetBytes aChar))
print 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
' Example of BitConverter class methods.
Module BitConverterDemo
Sub Main( )
Const formatter As String = "{0,25}{1,30}"
Dim aDoubl As Double = 0.1111111111111111111
Dim aSingl As Single = 0.1111111111111111111
Dim aLong As Long = 1111111111111111111
Dim anInt As Integer = 1111111111
Dim aShort As Short = 11111
Dim aChar As Char = "*"c
Dim aBool As Boolean = True
Console.WriteLine( _
"This example of methods of the BitConverter class" & _
vbCrLf & "generates the following output." & vbCrLf )
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 ) ) )
End Sub
End Module
' 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
注解
类 BitConverter 有助于以基本形式(作为一系列字节)操作值类型。 字节定义为 8 位无符号整数。 类 BitConverter 包括静态方法,用于将每个基元类型与字节数组相互转换,如下表所示。
如果使用 BitConverter 方法往返数据,请确保 GetBytes 重载和 To
Type 方法指定相同的类型。 如以下示例所示,通过调用 ToUInt32 方法还原表示带符号整数的数组可能会导致值与原始值不同。 有关详细信息,请参阅 使用带符号的非十进制值和按位值。
using System;
public class Example
{
public static void Main()
{
int value = -16;
Byte[] bytes = BitConverter.GetBytes(value);
// Convert bytes back to int.
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
open System
let value = -16
let bytes = BitConverter.GetBytes value
// Convert bytes back to int.
let intValue = BitConverter.ToInt32(bytes, 0)
printfn $"""{value} = {intValue}: {if value.Equals intValue then "Round-trips" else "Does not round-trip"}"""
// Convert bytes to UInt32.
let uintValue = BitConverter.ToUInt32(bytes, 0)
printfn $"""{value} = {uintValue}: {if value.Equals uintValue then "Round-trips" else "Does not round-trip"}"""
// The example displays the following output:
// -16 = -16: Round-trips
// -16 = 4294967280: Does not round-trip
Module Example
Public Sub Main()
Dim value As Integer = -16
Dim bytes() As Byte = BitConverter.GetBytes(value)
' Convert bytes back to Int32.
Dim intValue As Integer = BitConverter.ToInt32(bytes, 0)
Console.WriteLine("{0} = {1}: {2}",
value, intValue,
If(value.Equals(intValue), "Round-trips", "Does not round-trip"))
' Convert bytes to UInt32.
Dim uintValue As UInteger = BitConverter.ToUInt32(bytes, 0)
Console.WriteLine("{0} = {1}: {2}", value, uintValue,
If(value.Equals(uintValue), "Round-trips", "Does not round-trip"))
End Sub
End Module
' The example displays the following output:
' -16 = -16: Round-trips
' -16 = 4294967280: Does not round-trip
方法重 GetBytes 载的数组中字节的顺序 (以及方法) 返回 DoubleToInt64Bits 的整数中位数的顺序取决于计算机体系结构是 little-endian 还是 big-endian。 同样,数组中由 IntegerValue 方法和 ToChar 方法返回To
的字节顺序取决于计算机体系结构是 little-endian 还是 big-endian。 体系结构的结束度由 IsLittleEndian 属性指示,该属性在 little-endian 系统和 false
big-endian 系统上返回true
。 在 little-endian 系统上,低位字节先于高阶字节。 在 big-endian 系统上,高位字节位于低位字节之前。 下表说明了将整数 1,234,567,890 (0x499602D2) 传递给 GetBytes(Int32) 方法所导致的字节数组的差异。 字节按从索引 0 处的字节到索引 3 处的字节的顺序列出。
Little-endian | D2-02-96-49 |
Big-endian | 49-96-02-D2 |
由于某些方法的返回值取决于系统体系结构,因此在传输超出计算机边界的字节数据时要小心:
如果保证发送和接收数据的所有系统具有相同的结束度,则无需对数据执行任何操作。
如果发送和接收数据的系统可以具有不同的结束度,请始终按特定顺序传输数据。 这意味着数组中的字节顺序可能需要在发送之前或接收它们之后进行反转。 一种常见约定是 (big-endian 顺序) 以网络字节顺序传输数据。 以下示例提供了一个实现,用于按网络字节顺序发送整数值。
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
open System let value = 12345678 let bytes = BitConverter.GetBytes value printfn $"{BitConverter.ToString bytes}" if BitConverter.IsLittleEndian then Array.Reverse bytes printfn $"{BitConverter.ToString bytes}" // Call method to send byte stream across machine boundaries. // Receive byte stream from beyond machine boundaries. printfn $"{BitConverter.ToString bytes}" if BitConverter.IsLittleEndian then Array.Reverse bytes printfn $"{BitConverter.ToString bytes}" let result = BitConverter.ToInt32(bytes, 0) printfn $"Original value: {value}" printfn $"Returned value: {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
Module Example Public Sub Main() Dim value As Integer = 12345678 Dim bytes() As Byte = BitConverter.GetBytes(value) Console.WriteLine(BitConverter.ToString(bytes)) If BitConverter.IsLittleEndian Then Array.Reverse(bytes) End If 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 Then Array.Reverse(bytes) End If Console.WriteLine(BitConverter.ToString(bytes)) Dim result As Integer = BitConverter.ToInt32(bytes, 0) Console.WriteLine("Original value: {0}", value) Console.WriteLine("Returned value: {0}", result) End Sub End Module ' 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
如果发送和接收数据的系统可以具有不同的结束度,并且要传输的数据由带符号整数组成,请调用 IPAddress.HostToNetworkOrder 方法将数据转换为网络字节顺序,调用 IPAddress.NetworkToHostOrder 方法以将其转换为接收方所需的顺序。
字段
IsLittleEndian |
指示数据存储在此计算机体系结构中的字节顺序(“endianness”)。 |
方法
DoubleToInt64Bits(Double) |
将指定的双精度浮点数转换为 64 位有符号整数。 |
DoubleToUInt64Bits(Double) |
将指定的双精度浮点数转换为 64 位无符号整数。 |
GetBytes(Boolean) |
以字节数组的形式返回指定的布尔值。 |
GetBytes(Char) |
以字节数组的形式返回指定的 Unicode 字符值。 |
GetBytes(Double) |
以字节数组的形式返回指定的双精度浮点值。 |
GetBytes(Half) |
以字节数组的形式返回指定的半精度浮点值。 |
GetBytes(Int128) |
将基数据类型转换为一个字节数组以及将一个字节数组转换为基数据类型。 |
GetBytes(Int16) |
以字节数组的形式返回指定的 16 位有符号整数值。 |
GetBytes(Int32) |
以字节数组的形式返回指定 32 位有符号整数值。 |
GetBytes(Int64) |
以字节数组的形式返回指定 64 位带符号整数值。 |
GetBytes(Single) |
以字节数组的形式返回指定的单精度浮点值。 |
GetBytes(UInt128) |
将基数据类型转换为一个字节数组以及将一个字节数组转换为基数据类型。 |
GetBytes(UInt16) |
以字节数组的形式返回指定的 16 位无符号整数值。 |
GetBytes(UInt32) |
以字节数组的形式返回指定的 32 位无符号整数值。 |
GetBytes(UInt64) |
以字节数组的形式返回指定的 64 位无符号整数值。 |
HalfToInt16Bits(Half) |
将半精度浮点值转换为 16 位整数。 |
HalfToUInt16Bits(Half) |
将指定的半精度浮点数转换为 16 位无符号整数。 |
Int16BitsToHalf(Int16) |
将指定的 16 位有符号整数值重新解释为半精度浮点值。 |
Int32BitsToSingle(Int32) |
将指定的 32 位整数重新解释为单精度浮点值。 |
Int64BitsToDouble(Int64) |
将指定的 64 位带符号整数重新解释成双精度浮点数。 |
SingleToInt32Bits(Single) |
将单精度浮点值转换为整数。 |
SingleToUInt32Bits(Single) |
将指定的单精度浮点数转换为 32 位无符号整数。 |
ToBoolean(Byte[], Int32) |
返回由字节数组中指定位置的一个字节转换来的布尔值。 |
ToBoolean(ReadOnlySpan<Byte>) |
将只读字节范围转换为布尔值。 |
ToChar(Byte[], Int32) |
返回由字节数组中指定位置的两个字节转换来的 Unicode 字符。 |
ToChar(ReadOnlySpan<Byte>) |
将只读字节范围转换为字符。 |
ToDouble(Byte[], Int32) |
返回由字节数组中指定位置的八个字节转换来的双精度浮点数。 |
ToDouble(ReadOnlySpan<Byte>) |
将只读字节范围转换为双精度浮点值。 |
ToHalf(Byte[], Int32) |
返回从字节数组中指定位置的两个字节转换的半精度浮点数。 |
ToHalf(ReadOnlySpan<Byte>) |
将只读字节范围转换为半精度浮点值。 |
ToInt128(Byte[], Int32) |
将基数据类型转换为一个字节数组以及将一个字节数组转换为基数据类型。 |
ToInt128(ReadOnlySpan<Byte>) |
将基数据类型转换为一个字节数组以及将一个字节数组转换为基数据类型。 |
ToInt16(Byte[], Int32) |
返回由字节数组中指定位置的两个字节转换来的 16 位有符号整数。 |
ToInt16(ReadOnlySpan<Byte>) |
将只读字节范围转换为 16 位带符号整数。 |
ToInt32(Byte[], Int32) |
返回由字节数组中指定位置的四个字节转换来的 32 位有符号整数。 |
ToInt32(ReadOnlySpan<Byte>) |
将只读字节范围转换为 32 位带符号整数。 |
ToInt64(Byte[], Int32) |
返回由字节数组中指定位置的八个字节转换来的 64 位有符号整数。 |
ToInt64(ReadOnlySpan<Byte>) |
将只读字节范围转换为 64 位带符号整数。 |
ToSingle(Byte[], Int32) |
返回由字节数组中指定位置的四个字节转换来的单精度浮点数。 |
ToSingle(ReadOnlySpan<Byte>) |
将只读字节范围转换为单精度浮点值。 |
ToString(Byte[]) |
将指定的字节数组的每个元素的数值转换为它的等效十六进制字符串表示形式。 |
ToString(Byte[], Int32) |
将指定的字节子数组的每个元素的数值转换为它的等效十六进制字符串表示形式。 |
ToString(Byte[], Int32, Int32) |
将指定的字节子数组的每个元素的数值转换为它的等效十六进制字符串表示形式。 |
ToUInt128(Byte[], Int32) |
将基数据类型转换为一个字节数组以及将一个字节数组转换为基数据类型。 |
ToUInt128(ReadOnlySpan<Byte>) |
将基数据类型转换为一个字节数组以及将一个字节数组转换为基数据类型。 |
ToUInt16(Byte[], Int32) |
返回由字节数组中指定位置的两个字节转换来的 16 位无符号整数。 |
ToUInt16(ReadOnlySpan<Byte>) |
将只读字节范围转换为 16 位无符号整数。 |
ToUInt32(Byte[], Int32) |
返回由字节数组中指定位置的四个字节转换来的 32 位无符号整数。 |
ToUInt32(ReadOnlySpan<Byte>) |
将只读字节范围转换为 32 位无符号整数。 |
ToUInt64(Byte[], Int32) |
返回由字节数组中指定位置的八个字节转换来的 64 位无符号整数。 |
ToUInt64(ReadOnlySpan<Byte>) |
将字节转换为无符号长整数。 |
TryWriteBytes(Span<Byte>, Boolean) |
将布尔值转换为字节范围。 |
TryWriteBytes(Span<Byte>, Char) |
将字符转换为字节范围。 |
TryWriteBytes(Span<Byte>, Double) |
将双精度浮点值转换为字节范围。 |
TryWriteBytes(Span<Byte>, Half) |
将半精度浮点值转换为字节范围。 |
TryWriteBytes(Span<Byte>, Int128) |
将基数据类型转换为一个字节数组以及将一个字节数组转换为基数据类型。 |
TryWriteBytes(Span<Byte>, Int16) |
将 16 位带符号整数转换为字节范围。 |
TryWriteBytes(Span<Byte>, Int32) |
将 32 位带符号整数转换为字节范围。 |
TryWriteBytes(Span<Byte>, Int64) |
将 64 位带符号整数转换为字节范围。 |
TryWriteBytes(Span<Byte>, Single) |
将单精度浮点值转换为字节范围。 |
TryWriteBytes(Span<Byte>, UInt128) |
将基数据类型转换为一个字节数组以及将一个字节数组转换为基数据类型。 |
TryWriteBytes(Span<Byte>, UInt16) |
将无符号 16 位整数转换为字节范围。 |
TryWriteBytes(Span<Byte>, UInt32) |
将 32 位无符号整数转换为字节范围。 |
TryWriteBytes(Span<Byte>, UInt64) |
将无符号 64 位整数转换为字节范围。 |
UInt16BitsToHalf(UInt16) |
将指定的 16 位无符号整数转换为半精度浮点数。 |
UInt32BitsToSingle(UInt32) |
将指定的 32 位无符号整数转换为单精度浮点数。 |
UInt64BitsToDouble(UInt64) |
将指定的 64 位无符号整数转换为双精度浮点数。 |
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈