using System; using System.Runtime.InteropServices; namespace MiscUtil.Conversion { /// /// Equivalent of System.BitConverter, but with either endianness. /// public abstract class EndianBitConverter { #region Endianness of this converter /// /// Indicates the byte order ("endianess") in which data is converted using this class. /// /// /// 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. /// /// true if this converter is little-endian, false otherwise. public abstract bool IsLittleEndian(); /// /// Indicates the byte order ("endianess") in which data is converted using this class. /// public abstract Endianness Endianness { get; } #endregion #region Factory properties static LittleEndianBitConverter little = new LittleEndianBitConverter(); /// /// Returns a little-endian bit converter instance. The same instance is /// always returned. /// public static LittleEndianBitConverter Little { get { return little; } } static BigEndianBitConverter big = new BigEndianBitConverter(); /// /// Returns a big-endian bit converter instance. The same instance is /// always returned. /// public static BigEndianBitConverter Big { get { return big; } } #endregion #region Double/primitive conversions /// /// Converts the specified double-precision floating point number to a /// 64-bit signed integer. Note: the endianness of this converter does not /// affect the returned value. /// /// The number to convert. /// A 64-bit signed integer whose value is equivalent to value. public long DoubleToInt64Bits(double value) { return BitConverter.DoubleToInt64Bits(value); } /// /// Converts the specified 64-bit signed integer to a double-precision /// floating point number. Note: the endianness of this converter does not /// affect the returned value. /// /// The number to convert. /// A double-precision floating point number whose value is equivalent to value. public double Int64BitsToDouble (long value) { return BitConverter.Int64BitsToDouble(value); } /// /// Converts the specified single-precision floating point number to a /// 32-bit signed integer. Note: the endianness of this converter does not /// affect the returned value. /// /// The number to convert. /// A 32-bit signed integer whose value is equivalent to value. public int SingleToInt32Bits(float value) { return new Int32SingleUnion(value).AsInt32; } /// /// Converts the specified 32-bit signed integer to a single-precision floating point /// number. Note: the endianness of this converter does not /// affect the returned value. /// /// The number to convert. /// A single-precision floating point number whose value is equivalent to value. public float Int32BitsToSingle (int value) { return new Int32SingleUnion(value).AsSingle; } #endregion #region To(PrimitiveType) conversions /// /// Returns a Boolean value converted from one byte at a specified position in a byte array. /// /// An array of bytes. /// The starting position within value. /// true if the byte at startIndex in value is nonzero; otherwise, false. public bool ToBoolean (byte[] value, int startIndex) { CheckByteArgument(value, startIndex, 1); return BitConverter.ToBoolean(value, startIndex); } /// /// Returns a Unicode character converted from two bytes at a specified position in a byte array. /// /// An array of bytes. /// The starting position within value. /// A character formed by two bytes beginning at startIndex. public char ToChar (byte[] value, int startIndex) { return unchecked((char) (CheckedFromBytes(value, startIndex, 2))); } /// /// Returns a double-precision floating point number converted from eight bytes /// at a specified position in a byte array. /// /// An array of bytes. /// The starting position within value. /// A double precision floating point number formed by eight bytes beginning at startIndex. public double ToDouble (byte[] value, int startIndex) { return Int64BitsToDouble(ToInt64(value, startIndex)); } /// /// Returns a single-precision floating point number converted from four bytes /// at a specified position in a byte array. /// /// An array of bytes. /// The starting position within value. /// A single precision floating point number formed by four bytes beginning at startIndex. public float ToSingle (byte[] value, int startIndex) { return Int32BitsToSingle(ToInt32(value, startIndex)); } /// /// Returns a 16-bit signed integer converted from two bytes at a specified position in a byte array. /// /// An array of bytes. /// The starting position within value. /// A 16-bit signed integer formed by two bytes beginning at startIndex. public short ToInt16 (byte[] value, int startIndex) { return unchecked((short) (CheckedFromBytes(value, startIndex, 2))); } /// /// Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array. /// /// An array of bytes. /// The starting position within value. /// A 32-bit signed integer formed by four bytes beginning at startIndex. public int ToInt32 (byte[] value, int startIndex) { return unchecked((int) (CheckedFromBytes(value, startIndex, 4))); } /// /// Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array. /// /// An array of bytes. /// The starting position within value. /// A 64-bit signed integer formed by eight bytes beginning at startIndex. public long ToInt64 (byte[] value, int startIndex) { return CheckedFromBytes(value, startIndex, 8); } /// /// Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array. /// /// An array of bytes. /// The starting position within value. /// A 16-bit unsigned integer formed by two bytes beginning at startIndex. public ushort ToUInt16 (byte[] value, int startIndex) { return unchecked((ushort) (CheckedFromBytes(value, startIndex, 2))); } /// /// Returns a 32-bit unsigned integer converted from four bytes at a specified position in a byte array. /// /// An array of bytes. /// The starting position within value. /// A 32-bit unsigned integer formed by four bytes beginning at startIndex. public uint ToUInt32 (byte[] value, int startIndex) { return unchecked((uint) (CheckedFromBytes(value, startIndex, 4))); } /// /// Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a byte array. /// /// An array of bytes. /// The starting position within value. /// A 64-bit unsigned integer formed by eight bytes beginning at startIndex. public ulong ToUInt64 (byte[] value, int startIndex) { return unchecked((ulong) (CheckedFromBytes(value, startIndex, 8))); } /// /// Checks the given argument for validity. /// /// The byte array passed in /// The start index passed in /// The number of bytes required /// value is a null reference /// /// startIndex is less than zero or greater than the length of value minus bytesRequired. /// static void CheckByteArgument(byte[] value, int startIndex, int bytesRequired) { if (value==null) { throw new ArgumentNullException("value"); } if (startIndex < 0 || startIndex > value.Length-bytesRequired) { throw new ArgumentOutOfRangeException("startIndex"); } } /// /// Checks the arguments for validity before calling FromBytes /// (which can therefore assume the arguments are valid). /// /// The bytes to convert after checking /// The index of the first byte to convert /// The number of bytes to convert /// long CheckedFromBytes(byte[] value, int startIndex, int bytesToConvert) { CheckByteArgument(value, startIndex, bytesToConvert); return FromBytes(value, startIndex, bytesToConvert); } /// /// Convert the given number of bytes from the given array, from the given start /// position, into a long, using the bytes as the least significant part of the long. /// By the time this is called, the arguments have been checked for validity. /// /// The bytes to convert /// The index of the first byte to convert /// The number of bytes to use in the conversion /// The converted number protected abstract long FromBytes(byte[] value, int startIndex, int bytesToConvert); #endregion #region ToString conversions /// /// Returns a String converted from the elements of a byte array. /// /// An array of bytes. /// All the elements of value are converted. /// /// A String of hexadecimal pairs separated by hyphens, where each pair /// represents the corresponding element in value; for example, "7F-2C-4A". /// public static string ToString(byte[] value) { return BitConverter.ToString(value); } /// /// Returns a String converted from the elements of a byte array starting at a specified array position. /// /// An array of bytes. /// The starting position within value. /// The elements from array position startIndex to the end of the array are converted. /// /// A String of hexadecimal pairs separated by hyphens, where each pair /// represents the corresponding element in value; for example, "7F-2C-4A". /// public static string ToString(byte[] value, int startIndex) { return BitConverter.ToString(value, startIndex); } /// /// Returns a String converted from a specified number of bytes at a specified position in a byte array. /// /// An array of bytes. /// The starting position within value. /// The number of bytes to convert. /// The length elements from array position startIndex are converted. /// /// A String of hexadecimal pairs separated by hyphens, where each pair /// represents the corresponding element in value; for example, "7F-2C-4A". /// public static string ToString(byte[] value, int startIndex, int length) { return BitConverter.ToString(value, startIndex, length); } #endregion #region Decimal conversions /// /// Returns a decimal value converted from sixteen bytes /// at a specified position in a byte array. /// /// An array of bytes. /// The starting position within value. /// A decimal formed by sixteen bytes beginning at startIndex. public decimal ToDecimal (byte[] value, int startIndex) { // HACK: This always assumes four parts, each in their own endianness, // starting with the first part at the start of the byte array. // On the other hand, there's no real format specified... int[] parts = new int[4]; for (int i=0; i < 4; i++) { parts[i] = ToInt32(value, startIndex+i*4); } return new Decimal(parts); } /// /// Returns the specified decimal value as an array of bytes. /// /// The number to convert. /// An array of bytes with length 16. public byte[] GetBytes(decimal value) { byte[] bytes = new byte[16]; int[] parts = decimal.GetBits(value); for (int i=0; i < 4; i++) { CopyBytesImpl(parts[i], 4, bytes, i*4); } return bytes; } /// /// Copies the specified decimal value into the specified byte array, /// beginning at the specified index. /// /// A character to convert. /// The byte array to copy the bytes into /// The first index into the array to copy the bytes into public void CopyBytes(decimal value, byte[] buffer, int index) { int[] parts = decimal.GetBits(value); for (int i=0; i < 4; i++) { CopyBytesImpl(parts[i], 4, buffer, i*4+index); } } #endregion #region GetBytes conversions /// /// Returns an array with the given number of bytes formed /// from the least significant bytes of the specified value. /// This is used to implement the other GetBytes methods. /// /// The value to get bytes for /// The number of significant bytes to return byte[] GetBytes(long value, int bytes) { byte[] buffer = new byte[bytes]; CopyBytes(value, bytes, buffer, 0); return buffer; } /// /// Returns the specified Boolean value as an array of bytes. /// /// A Boolean value. /// An array of bytes with length 1. public byte[] GetBytes(bool value) { return BitConverter.GetBytes(value); } /// /// Returns the specified Unicode character value as an array of bytes. /// /// A character to convert. /// An array of bytes with length 2. public byte[] GetBytes(char value) { return GetBytes(value, 2); } /// /// Returns the specified double-precision floating point value as an array of bytes. /// /// The number to convert. /// An array of bytes with length 8. public byte[] GetBytes(double value) { return GetBytes(DoubleToInt64Bits(value), 8); } /// /// Returns the specified 16-bit signed integer value as an array of bytes. /// /// The number to convert. /// An array of bytes with length 2. public byte[] GetBytes(short value) { return GetBytes(value, 2); } /// /// Returns the specified 32-bit signed integer value as an array of bytes. /// /// The number to convert. /// An array of bytes with length 4. public byte[] GetBytes(int value) { return GetBytes(value, 4); } /// /// Returns the specified 64-bit signed integer value as an array of bytes. /// /// The number to convert. /// An array of bytes with length 8. public byte[] GetBytes(long value) { return GetBytes(value, 8); } /// /// Returns the specified single-precision floating point value as an array of bytes. /// /// The number to convert. /// An array of bytes with length 4. public byte[] GetBytes(float value) { return GetBytes(SingleToInt32Bits(value), 4); } /// /// Returns the specified 16-bit unsigned integer value as an array of bytes. /// /// The number to convert. /// An array of bytes with length 2. public byte[] GetBytes(ushort value) { return GetBytes(value, 2); } /// /// Returns the specified 32-bit unsigned integer value as an array of bytes. /// /// The number to convert. /// An array of bytes with length 4. public byte[] GetBytes(uint value) { return GetBytes(value, 4); } /// /// Returns the specified 64-bit unsigned integer value as an array of bytes. /// /// The number to convert. /// An array of bytes with length 8. public byte[] GetBytes(ulong value) { return GetBytes(unchecked((long)value), 8); } #endregion #region CopyBytes conversions /// /// Copies the given number of bytes from the least-specific /// end of the specified value into the specified byte array, beginning /// at the specified index. /// This is used to implement the other CopyBytes methods. /// /// The value to copy bytes for /// The number of significant bytes to copy /// The byte array to copy the bytes into /// The first index into the array to copy the bytes into void CopyBytes(long value, int bytes, byte[] buffer, int index) { if (buffer==null) { throw new ArgumentNullException("buffer", "Byte array must not be null"); } if (buffer.Length < index+bytes) { throw new ArgumentOutOfRangeException("Buffer not big enough for value"); } CopyBytesImpl(value, bytes, buffer, index); } /// /// Copies the given number of bytes from the least-specific /// end of the specified value into the specified byte array, beginning /// at the specified index. /// This must be implemented in concrete derived classes, but the implementation /// may assume that the value will fit into the buffer. /// /// The value to copy bytes for /// The number of significant bytes to copy /// The byte array to copy the bytes into /// The first index into the array to copy the bytes into protected abstract void CopyBytesImpl(long value, int bytes, byte[] buffer, int index); /// /// Copies the specified Boolean value into the specified byte array, /// beginning at the specified index. /// /// A Boolean value. /// The byte array to copy the bytes into /// The first index into the array to copy the bytes into public void CopyBytes(bool value, byte[] buffer, int index) { CopyBytes(value ? 1 : 0, 1, buffer, index); } /// /// Copies the specified Unicode character value into the specified byte array, /// beginning at the specified index. /// /// A character to convert. /// The byte array to copy the bytes into /// The first index into the array to copy the bytes into public void CopyBytes(char value, byte[] buffer, int index) { CopyBytes(value, 2, buffer, index); } /// /// Copies the specified double-precision floating point value into the specified byte array, /// beginning at the specified index. /// /// The number to convert. /// The byte array to copy the bytes into /// The first index into the array to copy the bytes into public void CopyBytes(double value, byte[] buffer, int index) { CopyBytes(DoubleToInt64Bits(value), 8, buffer, index); } /// /// Copies the specified 16-bit signed integer value into the specified byte array, /// beginning at the specified index. /// /// The number to convert. /// The byte array to copy the bytes into /// The first index into the array to copy the bytes into public void CopyBytes(short value, byte[] buffer, int index) { CopyBytes(value, 2, buffer, index); } /// /// Copies the specified 32-bit signed integer value into the specified byte array, /// beginning at the specified index. /// /// The number to convert. /// The byte array to copy the bytes into /// The first index into the array to copy the bytes into public void CopyBytes(int value, byte[] buffer, int index) { CopyBytes(value, 4, buffer, index); } /// /// Copies the specified 64-bit signed integer value into the specified byte array, /// beginning at the specified index. /// /// The number to convert. /// The byte array to copy the bytes into /// The first index into the array to copy the bytes into public void CopyBytes(long value, byte[] buffer, int index) { CopyBytes(value, 8, buffer, index); } /// /// Copies the specified single-precision floating point value into the specified byte array, /// beginning at the specified index. /// /// The number to convert. /// The byte array to copy the bytes into /// The first index into the array to copy the bytes into public void CopyBytes(float value, byte[] buffer, int index) { CopyBytes(SingleToInt32Bits(value), 4, buffer, index); } /// /// Copies the specified 16-bit unsigned integer value into the specified byte array, /// beginning at the specified index. /// /// The number to convert. /// The byte array to copy the bytes into /// The first index into the array to copy the bytes into public void CopyBytes(ushort value, byte[] buffer, int index) { CopyBytes(value, 2, buffer, index); } /// /// Copies the specified 32-bit unsigned integer value into the specified byte array, /// beginning at the specified index. /// /// The number to convert. /// The byte array to copy the bytes into /// The first index into the array to copy the bytes into public void CopyBytes(uint value, byte[] buffer, int index) { CopyBytes(value, 4, buffer, index); } /// /// Copies the specified 64-bit unsigned integer value into the specified byte array, /// beginning at the specified index. /// /// The number to convert. /// The byte array to copy the bytes into /// The first index into the array to copy the bytes into public void CopyBytes(ulong value, byte[] buffer, int index) { CopyBytes(unchecked((long)value), 8, buffer, index); } #endregion #region Private struct used for Single/Int32 conversions /// /// Union used solely for the equivalent of DoubleToInt64Bits and vice versa. /// [StructLayout(LayoutKind.Explicit)] struct Int32SingleUnion { /// /// Int32 version of the value. /// [FieldOffset(0)] int i; /// /// Single version of the value. /// [FieldOffset(0)] float f; /// /// Creates an instance representing the given integer. /// /// The integer value of the new instance. internal Int32SingleUnion(int i) { this.f = 0; // Just to keep the compiler happy this.i = i; } /// /// Creates an instance representing the given floating point number. /// /// The floating point value of the new instance. internal Int32SingleUnion(float f) { this.i = 0; // Just to keep the compiler happy this.f = f; } /// /// Returns the value of the instance as an integer. /// internal int AsInt32 { get { return i; } } /// /// Returns the value of the instance as a floating point number. /// internal float AsSingle { get { return f; } } } #endregion } }