System.BitConverter Class

Assembly: Mscorlib.dll
Namespace: System
Summary
Converts base data types to an array of bytes, and an array of bytes to base data types.
C# Syntax:
public sealed class BitConverter
Remarks
This class facilitates manipulating value types in their fundamental form. A byte is defined as an 8-bit unsigned integer.
See also:
System Namespace | Byte

System.BitConverter Member List:

Public Fields
IsLittleEndian Indicates the byte order ("endianess") in which data is stored in this computer architecture.
Public Methods
DoubleToInt64Bits Converts the specified double-precision floating point number to a 64-bit signed integer.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetBytes Overloaded:
GetBytes(bool value)

Returns the specified Boolean value as an array of bytes.
GetBytes Overloaded:
GetBytes(char value)

Returns the specified Unicode character value as an array of bytes.
GetBytes Overloaded:
GetBytes(double value)

Returns the specified double-precision floating point value as an array of bytes.
GetBytes Overloaded:
GetBytes(short value)

Returns the specified 16-bit signed integer value as an array of bytes.
GetBytes Overloaded:
GetBytes(int value)

Returns the specified 32-bit signed integer value as an array of bytes.
GetBytes Overloaded:
GetBytes(long value)

Returns the specified 64-bit signed integer value as an array of bytes.
GetBytes Overloaded:
GetBytes(float value)

Returns the specified single-precision floating point value as an array of bytes.
GetBytes Overloaded:
GetBytes(ushort value)

Returns the specified 16-bit unsigned integer value as an array of bytes.
GetBytes Overloaded:
GetBytes(uint value)

Returns the specified 32-bit unsigned integer value as an array of bytes.
GetBytes Overloaded:
GetBytes(ulong value)

Returns the specified 64-bit unsigned integer value as an array of bytes.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
Int64BitsToDouble Converts the specified 64-bit signed integer to a double-precision floating point number.
ToBoolean Returns a Boolean value converted from one byte at a specified position in a byte array.
ToChar Returns a Unicode character converted from two bytes at a specified position in a byte array.
ToDouble Returns a double-precision floating point number converted from eight bytes at a specified position in a byte array.
ToInt16 Returns a 16-bit signed integer converted from two bytes at a specified position in a byte array.
ToInt32 Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array.
ToInt64 Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array.
ToSingle Returns a single-precision floating point number converted from four bytes at a specified position in a byte array.
ToString
(inherited from System.Object)
Overloaded:
ToString()

See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
ToString Overloaded:
ToString(byte[] value)

Returns a String converted from the elements of a byte array.
ToString Overloaded:
ToString(byte[] value, int startIndex)

Returns a String converted from the elements of a byte array starting at a specified array position.
ToString Overloaded:
ToString(byte[] value, int startIndex, int length)

Returns a String converted from a specified number of bytes at a specified position in a byte array.
ToUInt16 Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array.
ToUInt32 Returns a 32-bit unsigned integer converted from four bytes at a specified position in a byte array.
ToUInt64 Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a byte array.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.BitConverter Member Details

Field: IsLittleEndian
Summary
Indicates the byte order ("endianess") in which data is stored in this computer architecture.
C# Syntax:
public static readonly bool IsLittleEndian;
Remarks
This value is true if the architecture is little-endian; false if it is big-endian.

Different computer architectures store data using different byte orders. "Big-endian" means the most significant byte is on the left end of a word. "Little-endian" means the most significant byte is on the right end of a word.

Return to top


Method: DoubleToInt64Bits(
   double value
)
Summary
Converts the specified double-precision floating point number to a 64-bit signed integer.
C# Syntax:
public static long DoubleToInt64Bits(
   double value
);
Parameters:

value

The number to convert.

Return Value:
A 64-bit signed integer whose value is equivalent to value.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~BitConverter();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: GetBytes(
   bool value
)
Summary
Returns the specified Boolean value as an array of bytes.
C# Syntax:
public static byte[] GetBytes(
   bool value
);
Parameters:

value

A Boolean value.

Return Value:
An array of bytes with length 1.

Return to top


Overloaded Method: GetBytes(
   char value
)
Summary
Returns the specified Unicode character value as an array of bytes.
C# Syntax:
public static byte[] GetBytes(
   char value
);
Parameters:

value

A character to convert.

Return Value:
An array of bytes with length 2.

Return to top


Overloaded Method: GetBytes(
   double value
)
Summary
Returns the specified double-precision floating point value as an array of bytes.
C# Syntax:
public static byte[] GetBytes(
   double value
);
Parameters:

value

The number to convert.

Return Value:
An array of bytes with length 8.

Return to top


Overloaded Method: GetBytes(
   short value
)
Summary
Returns the specified 16-bit signed integer value as an array of bytes.
C# Syntax:
public static byte[] GetBytes(
   short value
);
Parameters:

value

The number to convert.

Return Value:
An array of bytes with length 2.

Return to top


Overloaded Method: GetBytes(
   int value
)
Summary
Returns the specified 32-bit signed integer value as an array of bytes.
C# Syntax:
public static byte[] GetBytes(
   int value
);
Parameters:

value

The number to convert.

Return Value:
An array of bytes with length 4.

Return to top


Overloaded Method: GetBytes(
   long value
)
Summary
Returns the specified 64-bit signed integer value as an array of bytes.
C# Syntax:
public static byte[] GetBytes(
   long value
);
Parameters:

value

The number to convert.

Return Value:
An array of bytes with length 8.

Return to top


Overloaded Method: GetBytes(
   float value
)
Summary
Returns the specified single-precision floating point value as an array of bytes.
C# Syntax:
public static byte[] GetBytes(
   float value
);
Parameters:

value

The number to convert.

Return Value:
An array of bytes with length 4.

Return to top


Overloaded Method: GetBytes(
   ushort value
)
Summary
Returns the specified 16-bit unsigned integer value as an array of bytes.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public static byte[] GetBytes(
   ushort value
);
Parameters:

value

The number to convert.

Return Value:
An array of bytes with length 2.

Return to top


Overloaded Method: GetBytes(
   uint value
)
Summary
Returns the specified 32-bit unsigned integer value as an array of bytes.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public static byte[] GetBytes(
   uint value
);
Parameters:

value

The number to convert.

Return Value:
An array of bytes with length 4.

Return to top


Overloaded Method: GetBytes(
   ulong value
)
Summary
Returns the specified 64-bit unsigned integer value as an array of bytes.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public static byte[] GetBytes(
   ulong value
);
Parameters:

value

The number to convert.

Return Value:
An array of bytes with length 8.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Int64BitsToDouble(
   long value
)
Summary
Converts the specified 64-bit signed integer to a double-precision floating point number.
C# Syntax:
public static double Int64BitsToDouble(
   long value
);
Parameters:

value

The number to convert.

Return Value:
A double-precision floating point number whose value is equivalent to value.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToBoolean(
   byte[] value,
   int startIndex
)
Summary
Returns a Boolean value converted from one byte at a specified position in a byte array.
C# Syntax:
public static bool ToBoolean(
   byte[] value,
   int startIndex
);
Parameters:

value

An array of bytes.

startIndex

The starting position within value.

Return Value:
true if the byte at startIndex in value is nonzero; otherwise, false.
Exceptions
Exception Type Condition
ArgumentNullException value is null.
ArgumentOutOfRangeException startIndex is less than zero or greater than the length of value minus 1.

Return to top


Method: ToChar(
   byte[] value,
   int startIndex
)
Summary
Returns a Unicode character converted from two bytes at a specified position in a byte array.
C# Syntax:
public static char ToChar(
   byte[] value,
   int startIndex
);
Parameters:

value

An array.

startIndex

The starting position within value.

Return Value:
A character formed by two bytes beginning at startIndex.
Exceptions
Exception Type Condition
ArgumentNullException value is null.
ArgumentOutOfRangeException startIndex is less than zero or greater than the length of value minus 2.
See also:
Char

Return to top


Method: ToDouble(
   byte[] value,
   int startIndex
)
Summary
Returns a double-precision floating point number converted from eight bytes at a specified position in a byte array.
C# Syntax:
public static double ToDouble(
   byte[] value,
   int startIndex
);
Parameters:

value

An array of bytes.

startIndex

The starting position within value.

Return Value:
A double precision floating point number formed by eight bytes beginning at startIndex.
Exceptions
Exception Type Condition
ArgumentNullException value is null.
ArgumentOutOfRangeException startIndex is less than zero or greater than the length of value minus 8.

Return to top


Method: ToInt16(
   byte[] value,
   int startIndex
)
Summary
Returns a 16-bit signed integer converted from two bytes at a specified position in a byte array.
C# Syntax:
public static short ToInt16(
   byte[] value,
   int startIndex
);
Parameters:

value

An array of bytes.

startIndex

The starting position within value.

Return Value:
A 16-bit signed integer formed by two bytes beginning at startIndex.
Exceptions
Exception Type Condition
ArgumentNullException value is null.
ArgumentOutOfRangeException startIndex is less than zero or greater than the length of value minus 2.
See also:
Int16

Return to top


Method: ToInt32(
   byte[] value,
   int startIndex
)
Summary
Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array.
C# Syntax:
public static int ToInt32(
   byte[] value,
   int startIndex
);
Parameters:

value

An array of bytes.

startIndex

The starting position within value.

Return Value:
A 32-bit signed integer formed by four bytes beginning at startIndex.
Exceptions
Exception Type Condition
ArgumentNullException value is null.
ArgumentOutOfRangeException startIndex is less than zero or greater than the length of value minus 4.
See also:
Int32

Return to top


Method: ToInt64(
   byte[] value,
   int startIndex
)
Summary
Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array.
C# Syntax:
public static long ToInt64(
   byte[] value,
   int startIndex
);
Parameters:

value

An array of bytes.

startIndex

The starting position within value.

Return Value:
A 64-bit signed integer formed by eight bytes beginning at startIndex.
Exceptions
Exception Type Condition
ArgumentNullException value is null.
ArgumentOutOfRangeException startIndex is less than zero or greater than the length of value minus 8.
See also:
Int64

Return to top


Method: ToSingle(
   byte[] value,
   int startIndex
)
Summary
Returns a single-precision floating point number converted from four bytes at a specified position in a byte array.
C# Syntax:
public static float ToSingle(
   byte[] value,
   int startIndex
);
Parameters:

value

An array of bytes.

startIndex

The starting position within value.

Return Value:
A single-precision floating point number formed by four bytes beginning at startIndex.
Exceptions
Exception Type Condition
ArgumentNullException value is null.
ArgumentOutOfRangeException startIndex is less than zero or greater than the length of value minus 4.
See also:
Single

Return to top


Overloaded Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: ToString(
   byte[] value
)
Summary
Returns a String converted from the elements of a byte array.
C# Syntax:
public static string ToString(
   byte[] value
);
Parameters:

value

An array of bytes.

Return Value:
A String of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in value; for example, "7F-2C-4A".
Exceptions
Exception Type Condition
ArgumentNullException value is null.
Remarks
All the elements of value are converted.

Return to top


Overloaded Method: ToString(
   byte[] value,
   int startIndex
)
Summary
Returns a String converted from the elements of a byte array starting at a specified array position.
C# Syntax:
public static string ToString(
   byte[] value,
   int startIndex
);
Parameters:

value

An array of bytes.

startIndex

The starting position within value.

Return Value:
A String of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in value; for example, "7F-2C-4A".
Exceptions
Exception Type Condition
ArgumentNullException value is null.
ArgumentOutOfRangeException startIndex is less than zero or greater than the length of value minus 1.
Remarks
The elements from array position startIndex to the end of the array are converted.

Return to top


Overloaded Method: ToString(
   byte[] value,
   int startIndex,
   int length
)
Summary
Returns a String converted from a specified number of bytes at a specified position in a byte array.
C# Syntax:
public static string ToString(
   byte[] value,
   int startIndex,
   int length
);
Parameters:

value

An array of bytes.

startIndex

The starting position within value.

length

The number of bytes to convert.

Return Value:
A String of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in value; for example, "7F-2C-4A".
Exceptions
Exception Type Condition
ArgumentNullException value is null.
ArgumentOutOfRangeException startIndex or length is less than zero.

-or-

The sum of startIndex plus length is greater than the length of value.

Remarks
The length elements from array position startIndex are converted.

Return to top


Method: ToUInt16(
   byte[] value,
   int startIndex
)
Summary
Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public static ushort ToUInt16(
   byte[] value,
   int startIndex
);
Parameters:

value

The array of bytes.

startIndex

The starting position within value.

Return Value:
A 16-bit unsigned integer formed by two bytes beginning at startIndex.
Exceptions
Exception Type Condition
ArgumentNullException value is null.
ArgumentOutOfRangeException startIndex is less than zero or greater than the length of value minus 2.
See also:
UInt16

Return to top


Method: ToUInt32(
   byte[] value,
   int startIndex
)
Summary
Returns a 32-bit unsigned integer converted from four bytes at a specified position in a byte array.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public static uint ToUInt32(
   byte[] value,
   int startIndex
);
Parameters:

value

An array of bytes.

startIndex

The starting position within value.

Return Value:
A 32-bit unsigned integer formed by four bytes beginning at startIndex.
Exceptions
Exception Type Condition
ArgumentNullException value is null.
ArgumentOutOfRangeException startIndex is less than zero or greater than the length of value minus 4.
See also:
UInt32

Return to top


Method: ToUInt64(
   byte[] value,
   int startIndex
)
Summary
Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a byte array.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public static ulong ToUInt64(
   byte[] value,
   int startIndex
);
Parameters:

value

An array of bytes.

startIndex

The starting position within value.

Return Value:
A 64-bit unsigned integer formed by the eight bytes beginning at startIndex.
Exceptions
Exception Type Condition
ArgumentNullException value is null.
ArgumentOutOfRangeException startIndex is less than zero or greater than the length of value minus 8.
See also:
UInt64

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.