Mono Class Library: System Namespace

System.BitConverter Class

Use BitConverter class to work with binary representations of value types. [Edit]

See Also: BitConverter Members

public static class BitConverter

Remarks

This class returns a value type from an array of bytes or an array of bytes that is the binary representation of a value type. This lets you work with streams of bytes that are binary representations of value types, for example a file or a socket. The class also has a bool Field (BitConverter.IsLittleEndian) that indicates wherever the CPU is Little Endian or not.

In this example we convert the number 3516 (0x0DBC in hexadecimal) from binary to short. This code works only in a Little Endian Machine. If your Machine is Big Endian, change the indexs of the data Array.
C# Example
public class TestBitConverter 
{
	public static void Main() {
		// The hexadecimal representation of the number 3516 is 0x0DBC
		byte[] data = new byte[2];
		data[0] = 0xBC; // Little Endian Machine
		data[1] = 0x0D;
		
		short num = System.BitConverter.ToInt16(data, 0);
		System.Console.WriteLine("LittleEndian: {0}", System.BitConverter.IsLittleEndian? "yes" : "no" );
		System.Console.WriteLine("Number: {0}", num);
	}
}		
  
The output is:
Example
LittleEndian: yes
Number: 3516

[Edit]

Requirements

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