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
ToString(Byte[])
的十六进制字符串的顺序,) 取决于计算机体系结构是小端还是大端字符串。 同样,数组中的字节顺序并由
IntegerValue
方法返回
To
,该方法
ToChar
取决于计算机体系结构是小端还是 big-endian。 体系结构的尾部由属性指示,该属性在
IsLittleEndian
小端系统和
false
big-endian 系统上返回
true
。 在小端系统上,低顺序字节位于高阶字节之前。 在 big-endian 系统上,高阶字节在低阶字节之前。 下表演示了从将整数 1,234,567,890 (0x499602D2) 传递到
GetBytes(Int32)
方法导致的字节数组的差异。 字节按索引 0 处的字节到索引 3 处的字节的顺序列出。
如果所有发送和接收数据系统都保证具有相同的尾声,则不会对数据执行任何操作。
如果发送和接收数据的系统可以具有不同的尾声,请始终按特定顺序传输数据。 这意味着,数组中的字节顺序可能需要在发送字节之前或接收它们之后撤消。 一种常见约定是 (大端顺序) 以网络字节顺序传输数据。 以下示例提供一个实现,用于按网络字节顺序发送整数值。
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
将数据转换为接收方所需的顺序的方法。