Early Preview

This is currently very much a preview. Please feel free to try things out, but don't be upset if anything is not yet working. Feedback is welcome over on our GitHub Dicussions page.

class System.​BitConverter

Assembly: System.Runtime

Inheritance: object → BitConverter

Converts base data types to an array of bytes, and an array of bytes to base data types.

Fields and Constants

public static readonly bool
IsLittleEndian
Indicates the byte order ("endianness") in which data is stored in this computer architecture.

Methods

public static long
DoubleToInt64Bits​(double value)
Converts the specified double-precision floating point number to a 64-bit signed integer.
Returns A 64-bit signed integer whose value is equivalent to <paramref name="value" /> .
value The number to convert.
public static ulong
DoubleToUInt64Bits​(double value)
Converts the specified double-precision floating point number to a 64-bit unsigned integer.
Returns A 64-bit unsigned integer whose bits are identical to <paramref name="value" /> .
value The number to convert.
public static byte[]
GetBytes​(bool value)
Returns the specified Boolean value as a byte array.
Returns A byte array with length 1.
value The value to convert.
public static byte[]
GetBytes​(char value)
Returns the specified Unicode character value as an array of bytes.
Returns An array of bytes with length 2.
value The character to convert.
public static byte[]
GetBytes​(double value)
Returns the specified double-precision floating-point value as an array of bytes.
Returns An array of bytes with length 8.
value The number to convert.
public static byte[]
GetBytes​(Half value)
Returns the specified half-precision floating-point value as an array of bytes.
Returns An array of bytes with length 2.
value The number to convert.
public static byte[]
GetBytes​(Int128 value)
Returns the specified 128-bit signed integer value as an array of bytes.
Returns An array of bytes with length 16.
value The number to convert.
public static byte[]
GetBytes​(short value)
Returns the specified 16-bit signed integer value as an array of bytes.
Returns An array of bytes with length 2.
value The number to convert.
public static byte[]
GetBytes​(int value)
Returns the specified 32-bit signed integer value as an array of bytes.
Returns An array of bytes with length 4.
value The number to convert.
public static byte[]
GetBytes​(long value)
Returns the specified 64-bit signed integer value as an array of bytes.
Returns An array of bytes with length 8.
value The number to convert.
public static byte[]
GetBytes​(Single value)
Returns the specified single-precision floating point value as an array of bytes.
Returns An array of bytes with length 4.
value The number to convert.
public static byte[]
GetBytes​(UInt128 value)
Returns the specified 128-bit unsigned integer value as an array of bytes.
Returns An array of bytes with length 16.
value The number to convert.
public static byte[]
GetBytes​(ushort value)
Returns the specified 16-bit unsigned integer value as an array of bytes.
Returns An array of bytes with length 2.
value The number to convert.
public static byte[]
GetBytes​(uint value)
Returns the specified 32-bit unsigned integer value as an array of bytes.
Returns An array of bytes with length 4.
value The number to convert.
public static byte[]
GetBytes​(ulong value)
Returns the specified 64-bit unsigned integer value as an array of bytes.
Returns An array of bytes with length 8.
value The number to convert.
public static short
HalfToInt16Bits​(Half value)
Converts a half-precision floating-point value into a 16-bit integer.
Returns An integer representing the converted half-precision floating-point value.
value The half-precision floating-point value to convert.
public static ushort
HalfToUInt16Bits​(Half value)
Converts the specified half-precision floating point number to a 16-bit unsigned integer.
Returns A 16-bit unsigned integer whose bits are identical to <paramref name="value" /> .
value The number to convert.
public static Half
Int16BitsToHalf​(short value)
Reinterprets the specified 16-bit signed integer value as a half-precision floating-point value.
Returns A half-precision floating-point value that represents the converted integer.
value The 16-bit signed integer value to convert.
public static Single
Int32BitsToSingle​(int value)
Reinterprets the specified 32-bit integer as a single-precision floating-point value.
Returns A single-precision floating-point value that represents the converted integer.
value The integer to convert.
public static double
Int64BitsToDouble​(long value)
Reinterprets the specified 64-bit signed integer to a double-precision floating point number.
Returns A double-precision floating point number whose value is equivalent to <paramref name="value" /> .
value The number to convert.
public static int
SingleToInt32Bits​(Single value)
Converts a single-precision floating-point value into an integer.
Returns An integer representing the converted single-precision floating-point value.
value The single-precision floating-point value to convert.
public static uint
SingleToUInt32Bits​(Single value)
Converts the specified single-precision floating point number to a 32-bit unsigned integer.
Returns A 32-bit unsigned integer whose bits are identical to <paramref name="value" /> .
value The number to convert.
public static bool
ToBoolean​(byte[] value, int startIndex)
Returns a Boolean value converted from the byte at a specified position in a byte array.
Returns <see langword="true" /> if the byte at <paramref name="startIndex" /> in <paramref name="value" /> is nonzero; otherwise, <see langword="false" /> .
value A byte array.
startIndex The index of the byte within <paramref name="value" /> to convert.
public static bool
ToBoolean​(ReadOnlySpan<​byte> value)
Converts a read-only byte span to a Boolean value.
Returns A Boolean representing the converted bytes.
value A read-only span containing the bytes to convert.
public static char
ToChar​(byte[] value, int startIndex)
Returns a Unicode character converted from two bytes at a specified position in a byte array.
Returns The character formed by two bytes beginning at <paramref name="startIndex" /> .
value An array that includes the two bytes to convert.
startIndex The starting position within <paramref name="value" /> .
public static char
ToChar​(ReadOnlySpan<​byte> value)
Converts a read-only byte span into a character.
Returns A character representing the converted bytes.
value A read-only span containing the bytes to convert.
public static double
ToDouble​(byte[] value, int startIndex)
Returns a double-precision floating point number converted from eight bytes at a specified position in a byte array.
Returns A double-precision floating point number formed by eight bytes beginning at <paramref name="startIndex" /> .
value An array of bytes that includes the eight bytes to convert.
startIndex The starting position within <paramref name="value" /> .
public static double
ToDouble​(ReadOnlySpan<​byte> value)
Converts a read-only byte span into a double-precision floating-point value.
Returns A double-precision floating-point value that represents the converted bytes.
value A read-only span containing the bytes to convert.
public static Half
ToHalf​(byte[] value, int startIndex)
Returns a half-precision floating point number converted from two bytes at a specified position in a byte array.
Returns A half-precision floating point number formed by two bytes beginning at <paramref name="startIndex" /> .
value An array of bytes that includes the two bytes to convert.
startIndex The starting position within <paramref name="value" /> .
public static Half
ToHalf​(ReadOnlySpan<​byte> value)
Converts a read-only byte span into a half-precision floating-point value.
Returns A half-precision floating-point value that represents the converted bytes.
value A read-only span containing the bytes to convert.
public static Int128
ToInt128​(byte[] value, int startIndex)
Returns a 128-bit signed integer converted from sixteen bytes at a specified position in a byte array.
Returns A 128-bit signed integer formed by sixteen bytes beginning at <paramref name="startIndex" /> .
value An array of bytes.
startIndex The starting position within <paramref name="value" /> .
public static Int128
ToInt128​(ReadOnlySpan<​byte> value)
Converts a read-only byte span into a 128-bit signed integer.
Returns A 128-bit signed integer representing the converted bytes.
value A read-only span containing the bytes to convert.
public static short
ToInt16​(byte[] value, int startIndex)
Returns a 16-bit signed integer converted from two bytes at a specified position in a byte array.
Returns A 16-bit signed integer formed by two bytes beginning at <paramref name="startIndex" /> .
value An array of bytes that includes the two bytes to convert.
startIndex The starting position within <paramref name="value" /> .
public static short
ToInt16​(ReadOnlySpan<​byte> value)
Converts a read-only byte span into a 16-bit signed integer.
Returns A 16-bit signed integer representing the converted bytes.
value A read-only span containing the bytes to convert.
public static int
ToInt32​(byte[] value, int startIndex)
Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array.
Returns A 32-bit signed integer formed by four bytes beginning at <paramref name="startIndex" /> .
value An array of bytes that includes the four bytes to convert.
startIndex The starting position within <paramref name="value" /> .
public static int
ToInt32​(ReadOnlySpan<​byte> value)
Converts a read-only byte span into a 32-bit signed integer.
Returns A 32-bit signed integer representing the converted bytes.
value A read-only span containing the bytes to convert.
public static long
ToInt64​(byte[] value, int startIndex)
Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array.
Returns A 64-bit signed integer formed by eight bytes beginning at <paramref name="startIndex" /> .
value An array of bytes that includes the eight bytes to convert.
startIndex The starting position within <paramref name="value" /> .
public static long
ToInt64​(ReadOnlySpan<​byte> value)
Converts a read-only byte span into a 64-bit signed integer.
Returns A 64-bit signed integer representing the converted bytes.
value A read-only span containing the bytes to convert.
public static Single
ToSingle​(byte[] value, int startIndex)
Returns a single-precision floating point number converted from four bytes at a specified position in a byte array.
Returns A single-precision floating point number formed by four bytes beginning at <paramref name="startIndex" /> .
value An array of bytes.
startIndex The starting position within <paramref name="value" /> .
public static Single
ToSingle​(ReadOnlySpan<​byte> value)
Converts a read-only byte span into a single-precision floating-point value.
Returns A single-precision floating-point value representing the converted bytes.
value A read-only span containing the bytes to convert.
public static string
ToString​(byte[] value)
Converts the numeric value of each element of a specified array of bytes to its equivalent hexadecimal string representation.
Returns A string of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in <paramref name="value" /> ; for example, "7F-2C-4A-00".
value An array of bytes.
public static string
ToString​(byte[] value, int startIndex)
Converts the numeric value of each element of a specified subarray of bytes to its equivalent hexadecimal string representation.
Returns A string of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in a subarray of <paramref name="value" /> ; for example, "7F-2C-4A-00".
value An array of bytes.
startIndex The starting position within <paramref name="value" /> .
public static string
ToString​(byte[] value, int startIndex, int length)
Converts the numeric value of each element of a specified subarray of bytes to its equivalent hexadecimal string representation.
Returns A string of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in a subarray of <paramref name="value" /> ; for example, "7F-2C-4A-00".
value An array of bytes that includes the bytes to convert.
startIndex The starting position within <paramref name="value" /> .
length The number of array elements in <paramref name="value" /> to convert.
public static UInt128
ToUInt128​(byte[] value, int startIndex)
Returns a 128-bit unsigned integer converted from sixteen bytes at a specified position in a byte array.
Returns A 128-bit unsigned integer formed by sixteen bytes beginning at <paramref name="startIndex" /> .
value An array of bytes.
startIndex The starting position within <paramref name="value" /> .
public static UInt128
ToUInt128​(ReadOnlySpan<​byte> value)
Converts a read-only byte span into a 128-bit unsigned integer.
Returns A 128-bit unsigned integer representing the converted bytes.
value A read-only span containing the bytes to convert.
public static ushort
ToUInt16​(byte[] value, int startIndex)
Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array.
Returns A 16-bit unsigned integer formed by two bytes beginning at <paramref name="startIndex" /> .
value The array of bytes that includes the two bytes to convert.
startIndex The starting position within <paramref name="value" /> .
public static ushort
ToUInt16​(ReadOnlySpan<​byte> value)
Converts a read-only byte-span into a 16-bit unsigned integer.
Returns An 16-bit unsigned integer representing the converted bytes.
value A read-only span containing the bytes to convert.
public static uint
ToUInt32​(byte[] value, int startIndex)
Returns a 32-bit unsigned integer converted from four bytes at a specified position in a byte array.
Returns A 32-bit unsigned integer formed by four bytes beginning at <paramref name="startIndex" /> .
value An array of bytes.
startIndex The starting position within <paramref name="value" /> .
public static uint
ToUInt32​(ReadOnlySpan<​byte> value)
Converts a read-only byte span into a 32-bit unsigned integer.
Returns A 32-bit unsigned integer representing the converted bytes.
value A read-only span containing the bytes to convert.
public static ulong
ToUInt64​(byte[] value, int startIndex)
Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a byte array.
Returns A 64-bit unsigned integer formed by the eight bytes beginning at <paramref name="startIndex" /> .
value An array of bytes that includes the eight bytes to convert.
startIndex The starting position within <paramref name="value" /> .
public static ulong
ToUInt64​(ReadOnlySpan<​byte> value)
Converts bytes into an unsigned long.
Returns A 64-bit unsigned integer representing the converted bytes.
value A read-only span containing the bytes to convert.
public static bool
TryWriteBytes​(Span<​byte> destination, bool value)
Converts a Boolean into a span of bytes.
Returns <see langword="true" /> if the conversion was successful; <see langword="false" /> otherwise.
destination When this method returns, the bytes representing the converted Boolean.
value The Boolean to convert.
public static bool
TryWriteBytes​(Span<​byte> destination, char value)
Converts a character into a span of bytes.
Returns <see langword="true" /> if the conversion was successful; <see langword="false" /> otherwise.
destination When this method returns, the bytes representing the converted character.
value The character to convert.
public static bool
TryWriteBytes​(Span<​byte> destination, double value)
Converts a double-precision floating-point value into a span of bytes.
Returns <see langword="true" /> if the conversion was successful; <see langword="false" /> otherwise.
destination When this method returns, the bytes representing the converted double-precision floating-point value.
value The double-precision floating-point value to convert.
public static bool
TryWriteBytes​(Span<​byte> destination, Half value)
Converts a half-precision floating-point value into a span of bytes.
Returns <see langword="true" /> if the conversion was successful; <see langword="false" /> otherwise.
destination When this method returns, the bytes representing the converted half-precision floating-point value.
value The half-precision floating-point value to convert.
public static bool
TryWriteBytes​(Span<​byte> destination, Int128 value)
Converts a 128-bit signed integer into a span of bytes.
Returns <see langword="true" /> if the conversion was successful; <see langword="false" /> otherwise.
destination When this method returns, the bytes representing the converted 128-bit signed integer.
value The 128-bit signed integer to convert.
public static bool
TryWriteBytes​(Span<​byte> destination, short value)
Converts a 16-bit signed integer into a span of bytes.
Returns <see langword="true" /> if the conversion was successful; <see langword="false" /> otherwise.
destination When this method returns, the bytes representing the converted 16-bit signed integer.
value The 16-bit signed integer to convert.
public static bool
TryWriteBytes​(Span<​byte> destination, int value)
Converts a 32-bit signed integer into a span of bytes.
Returns <see langword="true" /> if the conversion was successful; <see langword="false" /> otherwise.
destination When this method returns, the bytes representing the converted 32-bit signed integer.
value The 32-bit signed integer to convert.
public static bool
TryWriteBytes​(Span<​byte> destination, long value)
Converts a 64-bit signed integer into a span of bytes.
Returns <see langword="true" /> if the conversion was successful; <see langword="false" /> otherwise.
destination When this method returns, the bytes representing the converted 64-bit signed integer.
value The 64-bit signed integer to convert.
public static bool
TryWriteBytes​(Span<​byte> destination, Single value)
Converts a single-precision floating-point value into a span of bytes.
Returns <see langword="true" /> if the conversion was successful; <see langword="false" /> otherwise.
destination When this method returns, the bytes representing the converted single-precision floating-point value.
value The single-precision floating-point value to convert.
public static bool
TryWriteBytes​(Span<​byte> destination, UInt128 value)
Converts a 128-bit unsigned integer into a span of bytes.
Returns <see langword="true" /> if the conversion was successful; <see langword="false" /> otherwise.
destination When this method returns, the bytes representing the converted 128-bit unsigned integer.
value The 128-bit unsigned integer to convert.
public static bool
TryWriteBytes​(Span<​byte> destination, ushort value)
Converts an unsigned 16-bit integer into a span of bytes.
Returns <see langword="true" /> if the conversion was successful; <see langword="false" /> otherwise.
destination When this method returns, the bytes representing the converted unsigned 16-bit integer.
value The unsigned 16-bit integer to convert.
public static bool
TryWriteBytes​(Span<​byte> destination, uint value)
Converts a 32-bit unsigned integer into a span of bytes.
Returns <see langword="true" /> if the conversion was successful; <see langword="false" /> otherwise.
destination When this method returns, the bytes representing the converted unsigned 32-bit integer.
value The unsigned 32-bit integer to convert.
public static bool
TryWriteBytes​(Span<​byte> destination, ulong value)
Converts an unsigned 64-bit integer into a span of bytes.
Returns <see langword="true" /> if the conversion was successful; <see langword="false" /> otherwise.
destination When this method returns, the bytes representing the converted unsigned 64-bit integer.
value The unsigned 64-bit integer to convert.
public static Half
UInt16BitsToHalf​(ushort value)
Converts the specified 16-bit unsigned integer to a half-precision floating point number.
Returns A half-precision floating point number whose bits are identical to <paramref name="value" /> .
value The number to convert.
public static Single
UInt32BitsToSingle​(uint value)
Converts the specified 32-bit unsigned integer to a single-precision floating point number.
Returns A single-precision floating point number whose bits are identical to <paramref name="value" /> .
value The number to convert.
public static double
UInt64BitsToDouble​(ulong value)
Converts the specified 64-bit unsigned integer to a double-precision floating point number.
Returns A double-precision floating point number whose bits are identical to <paramref name="value" /> .
value The number to convert.
public bool
Equals​(object obj)
Inherited from object
Determines whether the specified object is equal to the current object.
Returns <see langword="true" /> if the specified object is equal to the current object; otherwise, <see langword="false" /> .
obj The object to compare with the current object.
protected void
Finalize​()
Inherited from object
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
public int
GetHashCode​()
Inherited from object
Serves as the default hash function.
Returns A hash code for the current object.
public Type
GetType​()
Inherited from object
Gets the <see cref="T:System.Type" /> of the current instance.
Returns The exact runtime type of the current instance.
protected object
MemberwiseClone​()
Inherited from object
Creates a shallow copy of the current <see cref="T:System.Object" /> .
Returns A shallow copy of the current <see cref="T:System.Object" /> .
public string
ToString​()
Inherited from object
Returns a string that represents the current object.
Returns A string that represents the current object.