/// <summary> /// Convert the byte array into local representation from network byte order representation /// If the conditional compilation symbol _BIG_ENDIAN is defined, then it is assumed /// that the target machine is big endian else its assumed that the target machine /// is little endian /// </summary> /// <param name="data">A byte array</param> /// <returns>byte array in local byte order</returns> public static byte[] FromNetworkByteOrder(byte[] data) { if (data == null || data.Length < 2) { return(data); } if (AbstractByteUtils.IsTargetLittleEndian()) { data = AbstractByteUtils.ReverseBytes(data); } return(data); }
/// <summary> /// Convert the unsigned integer 16-bit to a byte array /// This conversion takes care of little/big endian-ness of the target machine /// </summary> /// <param name="data">The unsigned int 16-bit to convert to bytes</param> /// <returns>byte[]</returns> public static byte[] GetBytes(UInt16 data) { byte[] bs = new byte[2]; if (AbstractByteUtils.IsTargetLittleEndian()) { AbstractByteUtils.UInt16_To_LE(data, bs); } else { AbstractByteUtils.UInt16_To_BE(data, bs); } return(bs); }
/// <summary> /// Convert the incoming byte array to UInt64 using little-endian convention /// </summary> /// <param name="data">The byte array to be converted to unsigned int 64-bit</param> /// <param name="index">The index from where to pick up the data for conversion</param> /// <returns>UInt64</returns> public static UInt64 LE_To_UInt64(byte[] data, int index) { if (data == null) { throw new ArgumentNullException("Invalid argument(s) for conversion"); } if (data.Length < index + 8) { throw new ArgumentException("Invalid argument(s) for conversion"); } UInt32 lo = AbstractByteUtils.LE_To_UInt32(data); UInt32 hi = AbstractByteUtils.LE_To_UInt32(data, index); return((UInt64)(hi << 32) | lo); }
/// <summary> /// Convert the incoming byte array to UInt64 using big-endian convention /// </summary> /// <param name="data">The byte array to be converted to unsigned int 64-bit</param> /// <returns>UInt64</returns> public static UInt64 BE_To_UInt64(byte[] data) { if (data == null) { throw new ArgumentNullException("Invalid argument(s) for conversion"); } if (data.Length < 8) { throw new ArgumentException("Invalid argument(s) for conversion"); } UInt32 lo = AbstractByteUtils.BE_To_UInt32(data); UInt32 hi = AbstractByteUtils.BE_To_UInt32(data, 4); return((UInt64)(lo << 32) | hi); }
/// <summary> /// Convert the byte stream to UInt64 taking care of the endian-ness of the system. /// Null byte stream is returned as zero. Byte stream less than 8 bytes is still /// converted after appropriate padding with zeros based on endianness /// </summary> /// <param name="bs">The byte array to be converted to int 64-bit</param> /// <returns>UInt64</returns> public static UInt64 ToUInt64(byte[] bs) { if (bs == null) { return(0); } byte[] temp = new byte[8]; if (AbstractByteUtils.IsTargetLittleEndian()) { Array.Copy(bs, temp, bs.Length); return(AbstractByteUtils.LE_To_UInt64(temp, 0)); } else { Array.Copy(bs, 0, temp, 8 - bs.Length, bs.Length); return(AbstractByteUtils.BE_To_UInt64(bs, 0)); } }
/// <summary> /// Convert the long to a byte array (64-bit signed) /// This conversion takes care of little/big endian-ness of the target machine /// </summary> /// <param name="data">The long to convert to bytes</param> /// <returns>byte[]</returns> public static byte[] GetBytes(UInt64 data) { //We only need four bytes for this conversion byte[] asBytes = new byte[8]; asBytes[0] = (byte)(data & 0xFF); asBytes[1] = (byte)((data >> 8) & 0xFF); asBytes[2] = (byte)((data >> 16) & 0xFF); asBytes[3] = (byte)((data >> 24) & 0xFF); asBytes[4] = (byte)((data >> 32) & 0xFF); asBytes[5] = (byte)((data >> 40) & 0xFF); asBytes[6] = (byte)((data >> 48) & 0xFF); asBytes[7] = (byte)((data >> 56) & 0xFF); if (!AbstractByteUtils.IsTargetLittleEndian()) { asBytes = AbstractByteUtils.ReverseBytes(asBytes); } return(asBytes); }