See Also: BitConverter Members
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 | ||
char | ||
double |
-or- |
BitConverter.ToDouble(Byte[], int) -or- |
short | ||
int | ||
long | ||
float | ||
ushort | ||
uint | ||
ulong |
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:
If all systems sending and receiving data are guaranteed to have the same endianness, nothing has be done to the data.
If systems sending and receiving data can have different endianness, always transmit data in a particular order. This means that the order of bytes in the array may have to be reversed either before sending them or after receiving them. A common convention is to transmit data in network byte order (big-endian order). The following example provides an implementation for sending an integer value in network byte order.
code reference: System.BitConverter.Class#4
If systems sending and receiving data can have different endianness and the data to be transmitted consists of signed integers, call the System.Net.IPAddress.HostToNetworkOrder(int) method to convert the data to network byte order and the System.Net.IPAddress.NetworkToHostOrder(int) method to convert it to the order required by the recipient.