Posted by: Cirilo Meggiolaro | 02/9/2009

Tip of the day #118 – BitConverter class

Tip #117 showed how to use the CopyBlock method to copy block of bytes between arrays of primitive types.

Let’s check today the BitConverter class used to convert base data types to an array of bytes and vice versa.

The class has several methods that help you convert from and to a primitive type.

GetBytes Method

There are 10 overloads available for the GetBytes method. One for each primitive type that you can convert to an array of bytes:

public static byte[] GetBytes(bool value);
public static byte[] GetBytes(char value);
public static byte[] GetBytes(double value);
public static byte[] GetBytes(float value);
public static byte[] GetBytes(int value);
public static byte[] GetBytes(long value);
public static byte[] GetBytes(short value);
public static byte[] GetBytes(uint value);
public static byte[] GetBytes(ulong value);
public static byte[] GetBytes(ushort value);

Example

The following code converts an integer to an array of bytes and displays the bytes on the screen:

/// Declares an integer variable.
int a = 123;

/// Converts the integer to an array of bytes.
byte[] arr = BitConverter.GetBytes(a);

/// Loops through all elements in the array.
foreach (byte element in arr)
{
/// Displays the element.
Console.Write(“{0:X2} “, element);
}

Output: 7B 00 00 00

Methods to convert an array of bytes to primitive types

public static bool ToBoolean(byte[] value, int startIndex);
public static char ToChar(byte[] value, int startIndex);
public static double ToDouble(byte[] value, int startIndex);
public static short ToInt16(byte[] value, int startIndex);
public static int ToInt32(byte[] value, int startIndex);
public static long ToInt64(byte[] value, int startIndex);
public static float ToSingle(byte[] value, int startIndex);
public static ushort ToUInt16(byte[] value, int startIndex);
public static uint ToUInt32(byte[] value, int startIndex);
public static ulong ToUInt64(byte[] value, int startIndex);

Example

The following example converts an array of bytes to an integer.

/// Creates an array of bytes
byte[] arr = { 23, 0, 0, 0 };

/// Converts the array of bytes to an integer
int a = BitConverter.ToInt32(arr, 0);

/// Displays the result
Console.WriteLine(a);

Output: 23

String methods

There are 3 overloads available to convert an array of bytes to a hexadecimal string representation:

public static string ToString(byte[] value);
public static string ToString(byte[] value, int startIndex);
public static string ToString(byte[] value, int startIndex, int length);

Example

/// Creates an array of bytes
byte[] arr = { 23, 0, 0, 0 };

/// Converts the array of bytes to a string hexadecimal representation.
string s = BitConverter.ToString(arr);

/// Displays the result
Console.WriteLine(s);

Output: 17-00-00-00


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: