BitConverter


In programming you often need to convert a built-in data type into an array of bytes. For example, some hardware device might require an integer value, but that value must be sent one byte at a time. The reverse situation also frequently occurs. Sometimes data will be received as an ordered sequence of bytes that needs to be converted into one of the built-in types. For example, a device might output integers, sent as a stream of bytes. Whatever your conversion needs, C# provides the BitConverter class to meet them.

BitConverter contains the methods shown in Table 20-13. It defines the following field:

 public static readonly bool IsLittleEndian
Table 20-13: Methods Defined by BitConverter

Method

Meaning

public static long

DoubleToInt64Bits(double v)

Converts v into a long integer and returns the result.

public static byte[ ] GetBytes(bool v)

Converts v into a 1-byte array and returns the result.

public static byte[ ] GetBytes(char v)

Converts v into a 2-byte array and returns the result.

public static byte[ ] GetBytes(double v)

Converts v into an 8-byte array and returns the result.

public static byte[ ] GetBytes(float v)

Converts v into a 4-byte array and returns the result.

public static byte[ ] GetBytes(int v)

Converts v into a 4-byte array and returns the result.

public static byte[ ] GetBytes(long v)

Converts v into an 8-byte array and returns the result.

public static byte[ ] GetBytes(short v)

Converts v into a 2-byte array and returns the result.

public static byte[ ] GetBytes(uint v)

Converts v into a 4-byte array and returns the result.

public static byte[ ] GetBytes(ulong v)

Converts v into an 8-byte array and returns the result.

public static byte[ ] GetBytes(ushort v)

Converts v into a 2-byte array and returns the result.

public static double

Int64BitsToDouble(long v)

Converts v into a double floating-point value and returns the result.

public static bool ToBoolean(byte[ ] a, int idx)

Converts the byte at a[idx] into its bool equivalent and returns the result. A non-zero value is converted to true; zero is converted to false.

public static char ToChar(byte[ ] a, int start)

Converts two bytes starting at a[start] into its char equivalent and returns the result.

public static double ToDouble(byte[ ] a, int start)

Converts eight bytes starting at a[start] into its double equivalent and returns the result.

public static short ToInt16(byte[ ] a, int start)

Converts two bytes starting at a[start] into its short equivalent and returns the result.

public static int ToInt32(byte[ ] a, int start)

Converts four bytes starting at a[start] into its int equivalent and returns the result.

public static long ToInt64(byte[ ] a, int start)

Converts eight bytes starting at a[start] into its long equivalent and returns the result.

public static float ToSingle(byte[ ] a, int start)

Converts four bytes starting at a[start] into its float equivalent and returns the result.

public static string ToString(byte[ ] a)

Converts the bytes in a into a string. The string contains the hexadecimal values associated with the bytes, separated by hyphens.

public static string ToString(byte[ ] a, int start)

Converts the bytes in a, beginning at a[start], into a string. The string contains the hexadecimal values associated with the bytes, separated by hyphens.

public static string ToString(byte[ ] a, int start, int count)

Converts the bytes in a, beginning at a[start] and running for count bytes, into a string. The string contains the hexadecimal values associated with the bytes, separated by hyphens.

public static ushort ToUInt16(byte[ ] a, int start)

Converts two bytes starting at a[start] into its ushort equivalent and returns the result.

public static uint ToUInt32(byte[ ] a, int start)

Converts four bytes starting at a[start] into its uint equivalent and returns the result.

public static ulong ToUInt64(byte[ ] a, int start)

Converts eight bytes starting at a[start] into its ulong equivalent and returns the result.

This field is true if the current environment stores a word with the least significant byte first and the most significant byte second. This is called “little-endian” format. IsLittleEndian is false if the current environment stores a word with the most significant byte first and the least significant byte second. This is called “big-endian” format. Intel Pentiumbased machines use little-endian format.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net