Convertible Class
Definition
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
-
objectConvertible
Examples
The following example shows how to convert common .NET types to byte arrays using Convertible.GetBytes. It demonstrates converting an integer and a string, then restoring the integer from its byte representation.
using System;
using Cuemon;
namespace MyApp.Conversion;
public class ConvertibleExample
{
public void Demonstrate()
{
byte[] intBytes = Convertible.GetBytes(12345);
Console.WriteLine(intBytes.Length); // 4
Console.WriteLine(BitConverter.ToInt32(intBytes)); // 12345
byte[] stringBytes = Convertible.GetBytes("Hello");
Console.WriteLine(stringBytes.Length); // 5
int restored = BitConverter.ToInt32(intBytes);
Console.WriteLine(restored); // 12345
}
}
Fields
| Name | Description |
|---|---|
| BitsPerByte | Represents the number of bits in a byte. |
| BitsPerNibble | Represents the number of bits in a nibble. |
| NullValue | Represents a null value when converting to a byte array. |
Methods
| Name | Description |
|---|---|
| GetBytes(bool, Action<EndianOptions>) | Converts the specified bool value to its byte array representation. |
| GetBytes(byte, Action<EndianOptions>) | Converts the specified byte value to its byte array representation. |
| GetBytes(char, Action<EndianOptions>) | Converts the specified char value to its byte array representation. |
| GetBytes(IEnumerable<IConvertible>, Action<ConvertibleOptions>) | Converts the specified sequence of IConvertible values to a single aggregated byte array. |
| GetBytes(DBNull) | Converts the specified DBNull value to its byte array representation. |
| GetBytes(DateTime) | Converts the specified DateTime value to its byte array representation. |
| GetBytes(decimal) | Converts the specified decimal value to its byte array representation. |
| GetBytes(double, Action<EndianOptions>) | Converts the specified double value to its byte array representation. |
| GetBytes(Enum, Action<EndianOptions>) | Converts the specified Enum value to its underlying byte array representation. |
| GetBytes(IConvertible, Action<ConvertibleOptions>) | Converts the specified IConvertible value to its byte array representation. |
| GetBytes(short, Action<EndianOptions>) | Converts the specified short value to its byte array representation. |
| GetBytes(int, Action<EndianOptions>) | Converts the specified int value to its byte array representation. |
| GetBytes(long, Action<EndianOptions>) | Converts the specified long value to its byte array representation. |
| GetBytes(sbyte, Action<EndianOptions>) | Converts the specified sbyte value to its byte array representation. |
| GetBytes(float, Action<EndianOptions>) | Converts the specified float value to its byte array representation. |
| GetBytes(string, Action<EncodingOptions>) | Converts the specified string value to its byte array representation. |
| GetBytes(ushort, Action<EndianOptions>) | Converts the specified ushort value to its byte array representation. |
| GetBytes(uint, Action<EndianOptions>) | Converts the specified uint value to its byte array representation. |
| GetBytes(ulong, Action<EndianOptions>) | Converts the specified ulong value to its byte array representation. |
| RegisterConvertible<T>(Func<T, byte[]>) | Registers a custom converter for the specified IConvertible implementation. |
| ReverseBits16(ushort) | Reverses the bit order of the specified 16-bit unsigned integer. |
| ReverseBits32(uint) | Reverses the bit order of the specified 32-bit unsigned integer. |
| ReverseBits64(ulong) | Reverses the bit order of the specified 64-bit unsigned integer. |
| ReverseBits8(byte) | Reverses the bit order of the specified 8-bit unsigned integer. |
| ReverseEndianness(byte[], Action<EndianOptions>) | Reverses the byte order of the specified byte array when required by the configured endianness. |
| ToString(byte[], Action<EncodingOptions>) | Converts the specified byte array to its string representation. |