Class Convertible
- Namespace
- Cuemon
- Assembly
- Cuemon.Kernel.dll
Provides static helper methods for converting IConvertible values to and from byte arrays, including support for configurable encoding, byte order, and custom converters.
public static class Convertible
- Inheritance
-
Convertible
Fields
BitsPerByte
Represents the number of bits in a byte.
public const int BitsPerByte = 8
Field Value
BitsPerNibble
Represents the number of bits in a nibble.
public const int BitsPerNibble = 4
Field Value
NullValue
Represents a null value when converting to a byte array.
public const int NullValue = 0
Field Value
Methods
GetBytes(bool, Action<EndianOptions>)
Converts the specified bool value to its byte array representation.
public static byte[] GetBytes(bool input, Action<EndianOptions> setup = null)
Parameters
inputboolThe value to convert.
setupAction<EndianOptions>The delegate that configures the byte order.
Returns
- byte[]
A byte array that represents
input.
GetBytes(byte, Action<EndianOptions>)
Converts the specified byte value to its byte array representation.
public static byte[] GetBytes(byte input, Action<EndianOptions> setup = null)
Parameters
inputbyteThe value to convert.
setupAction<EndianOptions>The delegate that configures the byte order.
Returns
- byte[]
A byte array that represents
input.
GetBytes(char, Action<EndianOptions>)
Converts the specified char value to its byte array representation.
public static byte[] GetBytes(char input, Action<EndianOptions> setup = null)
Parameters
inputcharThe value to convert.
setupAction<EndianOptions>The delegate that configures the byte order.
Returns
- byte[]
A byte array that represents
input.
GetBytes(IEnumerable<IConvertible>, Action<ConvertibleOptions>)
Converts the specified sequence of IConvertible values to a single aggregated byte array.
public static byte[] GetBytes(IEnumerable<IConvertible> input, Action<ConvertibleOptions> setup = null)
Parameters
inputIEnumerable<IConvertible>The sequence of values to convert.
setupAction<ConvertibleOptions>The delegate that configures the conversion options.
Returns
- byte[]
A byte array containing the concatenated byte representations of the elements in
input.
GetBytes(DBNull)
Converts the specified DBNull value to its byte array representation.
public static byte[] GetBytes(DBNull input)
Parameters
inputDBNullThe value to convert.
Returns
GetBytes(DateTime)
Converts the specified DateTime value to its byte array representation.
public static byte[] GetBytes(DateTime input)
Parameters
inputDateTimeThe value to convert.
Returns
- byte[]
A byte array that represents
input.
Remarks
The value is formatted using the universal sortable date and time pattern and encoded with ASCII.
GetBytes(decimal)
Converts the specified decimal value to its byte array representation.
public static byte[] GetBytes(decimal input)
Parameters
inputdecimalThe value to convert.
Returns
- byte[]
A byte array that represents
input.
Remarks
The value is formatted using the invariant culture and encoded with ASCII.
GetBytes(double, Action<EndianOptions>)
Converts the specified double value to its byte array representation.
public static byte[] GetBytes(double input, Action<EndianOptions> setup = null)
Parameters
inputdoubleThe value to convert.
setupAction<EndianOptions>The delegate that configures the byte order.
Returns
- byte[]
A byte array that represents
input.
GetBytes(Enum, Action<EndianOptions>)
Converts the specified Enum value to its underlying byte array representation.
public static byte[] GetBytes(Enum input, Action<EndianOptions> setup = null)
Parameters
inputEnumThe value to convert.
setupAction<EndianOptions>The delegate that configures the byte order.
Returns
- byte[]
A byte array that represents the underlying numeric value of
input.
GetBytes(IConvertible, Action<ConvertibleOptions>)
Converts the specified IConvertible value to its byte array representation.
public static byte[] GetBytes(IConvertible input, Action<ConvertibleOptions> setup = null)
Parameters
inputIConvertibleThe value to convert.
setupAction<ConvertibleOptions>The delegate that configures the conversion options.
Returns
- byte[]
A byte array that represents
input.
Remarks
Returns the byte representation of NullValue when input is null.
Custom converters may be registered globally with RegisterConvertible<T>(Func<T, byte[]>) or supplied
locally through Converters.
Exceptions
- ArgumentOutOfRangeException
inputis of a type for which no converter has been registered or configured.
GetBytes(short, Action<EndianOptions>)
Converts the specified short value to its byte array representation.
public static byte[] GetBytes(short input, Action<EndianOptions> setup = null)
Parameters
inputshortThe value to convert.
setupAction<EndianOptions>The delegate that configures the byte order.
Returns
- byte[]
A byte array that represents
input.
GetBytes(int, Action<EndianOptions>)
Converts the specified int value to its byte array representation.
public static byte[] GetBytes(int input, Action<EndianOptions> setup = null)
Parameters
inputintThe value to convert.
setupAction<EndianOptions>The delegate that configures the byte order.
Returns
- byte[]
A byte array that represents
input.
GetBytes(long, Action<EndianOptions>)
Converts the specified long value to its byte array representation.
public static byte[] GetBytes(long input, Action<EndianOptions> setup = null)
Parameters
inputlongThe value to convert.
setupAction<EndianOptions>The delegate that configures the byte order.
Returns
- byte[]
A byte array that represents
input.
GetBytes(sbyte, Action<EndianOptions>)
Converts the specified sbyte value to its byte array representation.
public static byte[] GetBytes(sbyte input, Action<EndianOptions> setup = null)
Parameters
inputsbyteThe value to convert.
setupAction<EndianOptions>The delegate that configures the byte order.
Returns
- byte[]
A byte array that represents
input.
GetBytes(float, Action<EndianOptions>)
Converts the specified float value to its byte array representation.
public static byte[] GetBytes(float input, Action<EndianOptions> setup = null)
Parameters
inputfloatThe value to convert.
setupAction<EndianOptions>The delegate that configures the byte order.
Returns
- byte[]
A byte array that represents
input.
GetBytes(string, Action<EncodingOptions>)
Converts the specified string value to its byte array representation.
public static byte[] GetBytes(string input, Action<EncodingOptions> setup = null)
Parameters
inputstringThe value to convert.
setupAction<EncodingOptions>The delegate that configures the encoding behavior.
Returns
- byte[]
A byte array that represents
input.
Remarks
EncodingOptions is initialized with DefaultPreambleSequence and DefaultEncoding.
Exceptions
- ArgumentNullException
inputis null.- InvalidEnumArgumentException
setupconfigures an invalid value for Preamble.
GetBytes(ushort, Action<EndianOptions>)
Converts the specified ushort value to its byte array representation.
public static byte[] GetBytes(ushort input, Action<EndianOptions> setup = null)
Parameters
inputushortThe value to convert.
setupAction<EndianOptions>The delegate that configures the byte order.
Returns
- byte[]
A byte array that represents
input.
GetBytes(uint, Action<EndianOptions>)
Converts the specified uint value to its byte array representation.
public static byte[] GetBytes(uint input, Action<EndianOptions> setup = null)
Parameters
inputuintThe value to convert.
setupAction<EndianOptions>The delegate that configures the byte order.
Returns
- byte[]
A byte array that represents
input.
GetBytes(ulong, Action<EndianOptions>)
Converts the specified ulong value to its byte array representation.
public static byte[] GetBytes(ulong input, Action<EndianOptions> setup = null)
Parameters
inputulongThe value to convert.
setupAction<EndianOptions>The delegate that configures the byte order.
Returns
- byte[]
A byte array that represents
input.
RegisterConvertible<T>(Func<T, byte[]>)
Registers a custom converter for the specified IConvertible implementation.
public static void RegisterConvertible<T>(Func<T, byte[]> converter) where T : IConvertible
Parameters
Type Parameters
TThe type of the IConvertible implementation to register.
Exceptions
- ArgumentNullException
converteris null.
ReverseBits16(ushort)
Reverses the bit order of the specified 16-bit unsigned integer.
public static ushort ReverseBits16(ushort input)
Parameters
inputushortThe value whose bits to reverse.
Returns
ReverseBits32(uint)
Reverses the bit order of the specified 32-bit unsigned integer.
public static uint ReverseBits32(uint input)
Parameters
inputuintThe value whose bits to reverse.
Returns
ReverseBits64(ulong)
Reverses the bit order of the specified 64-bit unsigned integer.
public static ulong ReverseBits64(ulong input)
Parameters
inputulongThe value whose bits to reverse.
Returns
ReverseBits8(byte)
Reverses the bit order of the specified 8-bit unsigned integer.
public static byte ReverseBits8(byte input)
Parameters
inputbyteThe value whose bits to reverse.
Returns
ReverseEndianness(byte[], Action<EndianOptions>)
Reverses the byte order of the specified byte array when required by the configured endianness.
public static byte[] ReverseEndianness(byte[] input, Action<EndianOptions> setup = null)
Parameters
inputbyte[]The byte array whose byte order to reverse.
setupAction<EndianOptions>The delegate that configures the desired byte order.
Returns
- byte[]
input, either unchanged or reversed to match the configured byte order.
ToString(byte[], Action<EncodingOptions>)
Converts the specified byte array to its string representation.
public static string ToString(byte[] input, Action<EncodingOptions> setup = null)
Parameters
inputbyte[]The byte array to convert.
setupAction<EncodingOptions>The delegate that configures the encoding behavior.
Returns
- string
A string that represents
input.
Remarks
EncodingOptions is initialized with DefaultPreambleSequence and DefaultEncoding. If the configured encoding is the default encoding, the encoding is detected from the byte order mark when possible.
Exceptions
- ArgumentNullException
inputis null.- InvalidEnumArgumentException
setupconfigures an invalid value for Preamble.