System.BitConverter Class

Converts base data types to an array of bytes, and an array of bytes to base data types.

See Also: BitConverter Members

Syntax

public static class BitConverter

Remarks

The BitConverter class helps manipulate value types in their fundamental form, as a series of bytes. A byte is defined as an 8-bit unsigned integer. The BitConverter class includes static methods to convert each of the primitive types to and from an array of bytes, as the following table illustrates.

bool

BitConverter.GetBytes(bool)

BitConverter.ToBoolean(Byte[], int)

char

BitConverter.GetBytes(char)

BitConverter.ToChar(Byte[], int)

double

BitConverter.GetBytes(double)

-or-

BitConverter.DoubleToInt64Bits(double)

BitConverter.ToDouble(Byte[], int)

-or-

BitConverter.Int64BitsToDouble(long)

short

BitConverter.GetBytes(short)

BitConverter.ToInt16(Byte[], int)

int

BitConverter.GetBytes(int)

BitConverter.ToInt32(Byte[], int)

long

BitConverter.GetBytes(long)

BitConverter.ToInt64(Byte[], int)

float

BitConverter.GetBytes(float)

BitConverter.ToSingle(Byte[], int)

ushort

BitConverter.GetBytes(ushort)

BitConverter.ToUInt16(Byte[], int)

uint

BitConverter.GetBytes(uint)

BitConverter.ToUInt32(Byte[], int)

ulong

BitConverter.GetBytes(ulong)

BitConverter.ToUInt64(Byte[], int)

If you use BitConverter methods to round-trip data, make sure that the erload:System.BitConverter.GetBytes overload and the ToType method specify the same type. As the following example illustrates, restoring an array that represents a signed integer by calling the BitConverter.ToUInt32(Byte[], int) method can result in a value that is different from the original. For more information, see the entry tp://go.microsoft.com/fwlink/?LinkId=186999 in the BCL Team Blog.

code reference: System.BitConverter.Class#3

The order of bytes in the array returned by the erload:System.BitConverter.GetBytes method overloads (as well as the order of bits in the integer returned by the BitConverter.DoubleToInt64Bits(double) method and the order of hexadecimal strings returned by the BitConverter.ToString(Byte[]) method) depends on whether the computer architecture is little-endian or big-endian. Similarly, the order of bytes in the array and returned by the ToIntegerValue methods and the BitConverter.ToChar(Byte[], int) method depends on whether the computer architecture is little-endian or big-endian. The endianness of an architecture is indicated by the BitConverter.IsLittleEndian property, which returns true on little-endian systems and false on big-endian systems. On little-endian systems, lower-order bytes precede higher-order bytes. On big-endian system, higher-order bytes precede lower-order bytes. The following table illustrates the difference in the byte arrays that result from passing the integer 1,234,567,890 (0x499602D2) to the BitConverter.GetBytes(int) method. The bytes are listed in order from the byte at index 0 to the byte at index 3.

Little-endian

D2-02-96-49

Big-endian

49-96-02-D2

Because the return value of some methods depends on system architecture, be careful when transmitting byte data beyond machine boundaries:

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0