Exemple #1
0
        public static void WriteWord(this MemoryStream myMemoryStream, UInt16 myWord, ByteOrder myByteOrder = ByteOrder.Unmodified)
        {

            Byte[] _Bytes = null;

            switch (myByteOrder)
            {

                case ByteOrder.Unmodified:
                    _Bytes = BitConverter.GetBytes(myWord);
                    break;

                case ByteOrder.HostToNetwork:
                    _Bytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(myWord));
                    break;

                case ByteOrder.NetworkToHost:
                    _Bytes = BitConverter.GetBytes(IPAddress.NetworkToHostOrder(myWord));
                    break;

            }

            myMemoryStream.WriteByte(_Bytes[0]);  // high byte
            myMemoryStream.WriteByte(_Bytes[1]);  // low  byte

        }
        /// <summary>
        /// Applies the specified endianess to an int32 (reverse bytes if input endianess is different from system's endianess)
        /// </summary>
        /// <param name="byteOrder">the endianess to apply</param>
        /// <param name="value">the value to be converted</param>
        /// <returns>The value with applied endainess</returns>
        public static int ApplyInt32(ByteOrder byteOrder, int value)
        {
            if (byteOrder == NativeByteOrder) return value;

            return (int)((value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
                   (value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24);
        }
        /// <summary>
        /// Applies the specified endianess to an uint32 (reverse bytes if input endianess is different from system's endianess)
        /// </summary>
        /// <param name="byteOrder">the endianess to apply</param>
        /// <param name="value">the value to be converted</param>
        /// <returns>The value with applied endainess</returns>
        public static uint ApplyUint32(ByteOrder byteOrder, uint value)
        {
            if (byteOrder == NativeByteOrder) return value;

            return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
                   (value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;
        }
        /// <summary>
        /// Applies the specified endianess to an uint64 (reverse bytes if input endianess is different from system's endianess)
        /// </summary>
        /// <param name="byteOrder">the endianess to apply</param>
        /// <param name="value">the value to be converted</param>
        /// <returns>The value with applied endainess</returns>
        public static ulong ApplyUint64(ByteOrder byteOrder, ulong value)
        {
            if (byteOrder == NativeByteOrder) return value;

            return (value & 0x00000000000000FFUL) << 56 | (value & 0x000000000000FF00UL) << 40 |
                    (value & 0x0000000000FF0000UL) << 24 | (value & 0x00000000FF000000UL) << 8 |
                    (value & 0x000000FF00000000UL) >> 8 | (value & 0x0000FF0000000000UL) >> 24 |
                    (value & 0x00FF000000000000UL) >> 40 | (value & 0xFF00000000000000UL) >> 56;
        }
        public static unsafe float ApplyFloat(ByteOrder byteOrder, float value)
        {
            if (byteOrder == NativeByteOrder) return value;

            int valueInt = *(int*) &value;
            int applied = ApplyInt32(byteOrder, valueInt);

            return *(float*) &applied;
        }
Exemple #6
0
        public static unsafe byte[] GetBytes(float value, ByteOrder order)
        {
            byte[] bytes = GetBytes(value);
            if (order != ByteOrder.LittleEndian)
            {
                //reserver order
                ReverseArray(bytes);
            }

            return bytes;
        }
 public static short ToHostOrder(short value, ByteOrder inputByteOrder)
 {
     switch (inputByteOrder)
     {
         case ByteOrder.Host:
             return value;
         case ByteOrder.Network:
             return IPAddress.NetworkToHostOrder(value);
         default:
             throw new ArgumentOutOfRangeException("inputByteOrder");
     }
 }
		/// <summary>
		/// Converts the specified bytes to an Int32. Byter order is considered.
		/// </summary>
		/// <param name="byteOrder">The byte order to use during the conversion.</param>
		/// <param name="bytes">The source byte array from which the bytes will be taken for conversion.</param>
		/// <param name="srcOffset">The offset into the source array where the bytes will be taken.</param>
		/// <returns></returns>
		public static int ToInt32(ByteOrder byteOrder, byte[] bytes, int srcOffset)
		{
			if (bytes == null)
			{
				throw new ArgumentNullException("bytes");
			}

			int value = BitConverter.ToInt32(bytes, srcOffset);

			if (byteOrder == ByteOrder.BigEndian)
			{
				return IPAddress.NetworkToHostOrder(value);
			}

			return value;
		}
 public static double ToOutputOrder(double value, ByteOrder outputByteOrder)
 {
     switch (outputByteOrder)
     {
         case ByteOrder.Host:
             return value;
         case ByteOrder.Network:
             return LongToDouble(IPAddress.HostToNetworkOrder(DoubleToLong(value)));
         default:
             throw new ArgumentOutOfRangeException("outputByteOrder");
     }
 }
 public static float ToOutputOrder(float value, ByteOrder outputByteOrder)
 {
     switch (outputByteOrder)
     {
         case ByteOrder.Host:
             return value;
         case ByteOrder.Network:
             return IntegerToFloat(IPAddress.HostToNetworkOrder(FloatToInteger(value)));
         default:
             throw new ArgumentOutOfRangeException("outputByteOrder");
     }
 }
 public static long ToOutputOrder(long value, ByteOrder outputByteOrder)
 {
     switch (outputByteOrder)
     {
         case ByteOrder.Host:
             return value;
         case ByteOrder.Network:
             return IPAddress.HostToNetworkOrder(value);
         default:
             throw new ArgumentOutOfRangeException("outputByteOrder");
     }
 }
 public static ushort ToHostOrder(ushort value, ByteOrder inputByteOrder)
 {
     switch (inputByteOrder)
     {
         case ByteOrder.Host:
             return value;
         case ByteOrder.Network:
             return SignedToUnsignedShort(IPAddress.NetworkToHostOrder(UnsignedToSignedShort(value)));
         default:
             throw new ArgumentOutOfRangeException("inputByteOrder");
     }
 }
		/// <summary>
		/// Converts the specified bytes to a Guid. Byter order is considered.
		/// </summary>
		/// <param name="byteOrder">The byte order to use during the conversion.</param>
		/// <param name="bytes">The source byte array from which the bytes will be taken for conversion.</param>
		/// <param name="srcOffset">The offset into the source array where the bytes will be taken.</param>
		/// <returns></returns>
		public static Guid ToGuid(ByteOrder byteOrder, byte[] bytes, int srcOffset)
		{
			if (bytes == null)
			{
				throw new ArgumentNullException("bytes");
			}

			int  segmentA = BitConverter.ToInt32(bytes, srcOffset);
			short segmentB = BitConverter.ToInt16(bytes, srcOffset + 4);
			short segmentC = BitConverter.ToInt16(bytes, srcOffset + 6);

			if (byteOrder == ByteOrder.BigEndian)
			{	
				segmentA = IPAddress.NetworkToHostOrder(segmentA);
				segmentB = IPAddress.NetworkToHostOrder(segmentB);
				segmentC = IPAddress.NetworkToHostOrder(segmentC);
			}
			
			byte[] resultBytes = new byte[16];
			
			// copy the bytes around to recreate the Guid
			Buffer.BlockCopy(BitConverter.GetBytes(segmentA), 0, resultBytes, 0, 4); 
			Buffer.BlockCopy(BitConverter.GetBytes(segmentB), 0, resultBytes, 4, 2); 
			Buffer.BlockCopy(BitConverter.GetBytes(segmentC), 0, resultBytes, 6, 2); 
            Buffer.BlockCopy(bytes, srcOffset + 8, resultBytes, 8, 8); 

			return new Guid(resultBytes);
		}		
Exemple #14
0
        public static unsafe float ToSingle(byte[] value, int index, ByteOrder order)
        {
            if (order != ByteOrder.LittleEndian)
            {
                //reserver order
                ReverseArray(value);
            }

            return ToSingle(value, index);
        }
        /// <summary>
        /// Applies the specified endianess to an int64 (reverse bytes if input endianess is different from system's endianess)
        /// </summary>
        /// <param name="byteOrder">the endianess to apply</param>
        /// <param name="value">the value to be converted</param>
        /// <returns>The value with applied endainess</returns>
        public static long ApplyInt64(ByteOrder byteOrder, long value)
        {
            if (byteOrder == NativeByteOrder) return value;

            return IPAddress.HostToNetworkOrder(value);
        }
Exemple #16
0
        internal double ReadDouble(ByteOrder bo)
        {
            Contract.Requires(ReadBytesLeft >= sizeof(double));

            if (BitConverter.IsLittleEndian == (ByteOrder.LSB == bo))
            {
                var result = BitConverter.ToDouble(_buf, ReadPosition);
                ReadPosition += 8;
                return result;
            }
            else
            {
                _workspace[7] = _buf[ReadPosition++];
                _workspace[6] = _buf[ReadPosition++];
                _workspace[5] = _buf[ReadPosition++];
                _workspace[4] = _buf[ReadPosition++];
                _workspace[3] = _buf[ReadPosition++];
                _workspace[2] = _buf[ReadPosition++];
                _workspace[1] = _buf[ReadPosition++];
                _workspace[0] = _buf[ReadPosition++];
                return BitConverter.ToDouble(_workspace, 0);
            }
        }
		/// <summary>
		/// Converts the specified Int64 into a byte array. Byter order is considered.
		/// </summary>
		/// <param name="byteOrder">The byte order to use during the conversion.</param>
		/// <param name="value">The long to convert.</param>
		/// <returns></returns>
		public static byte[] GetBytes(ByteOrder byteOrder, long value)
		{
			if (byteOrder == ByteOrder.BigEndian)
			{
				value = IPAddress.HostToNetworkOrder(value);
			}

			return BitConverter.GetBytes(value);
		}
		/// <summary>
		/// Converts the specified Guid into a byte array. Byter order is considered.
		/// </summary>
		/// <param name="byteOrder">The byte order to use during the conversion.</param>
		/// <param name="value">The Guid to convert.</param>
		/// <returns></returns>
		public static byte[] GetBytes(ByteOrder byteOrder, Guid value)
		{
			byte[] bytes = value.ToByteArray();

			if (byteOrder == ByteOrder.BigEndian)
			{
				int  segmentA = BitConverter.ToInt32(bytes, 0);
				short segmentB = BitConverter.ToInt16(bytes, 4);
				short segmentC = BitConverter.ToInt16(bytes, 6);

				segmentA = IPAddress.HostToNetworkOrder(segmentA);
				segmentB = IPAddress.HostToNetworkOrder(segmentB);
				segmentC = IPAddress.HostToNetworkOrder(segmentC);

				Buffer.BlockCopy(BitConverter.GetBytes(segmentA), 0, bytes, 0, 4);
				Buffer.BlockCopy(BitConverter.GetBytes(segmentB), 0, bytes, 4, 2);
				Buffer.BlockCopy(BitConverter.GetBytes(segmentC), 0, bytes, 6, 2);
			}

			return bytes;
		}
        /// <summary>
        /// Applies the specified endianess to an uint16 (reverse bytes if input endianess is different from system's endianess)
        /// </summary>
        /// <param name="byteOrder">the endianess to apply</param>
        /// <param name="value">the value to be converted</param>
        /// <returns>The value with applied endainess</returns>
        public static ushort ApplyUint16(ByteOrder byteOrder, ushort value)
        {
            if (byteOrder == NativeByteOrder) return value;

            return (ushort)((value & 0xFFU) << 8 | (value & 0xFF00U) >> 8);
        }
        /// <summary>
        /// Applies the specified endianess to a double (reverse bytes if input endianess is different from system's endianess)
        /// </summary>
        /// <param name="byteOrder">the endianess to apply</param>
        /// <param name="value">the value to be converted</param>
        /// <returns>The value with applied endainess</returns>
        public static double ApplyDouble(ByteOrder byteOrder, double value)
        {
            if (byteOrder == NativeByteOrder) return value;

            return BitConverter.Int64BitsToDouble(IPAddress.HostToNetworkOrder(BitConverter.DoubleToInt64Bits(value)));
        }
 public static ulong ToOutputOrder(ulong value, ByteOrder outputByteOrder)
 {
     switch (outputByteOrder)
     {
         case ByteOrder.Host:
             return value;
         case ByteOrder.Network:
             return SignedToUnsignedLong(IPAddress.HostToNetworkOrder(UnsignedToSignedLong(value)));
         default:
             throw new ArgumentOutOfRangeException("outputByteOrder");
     }
 }
		/// <summary>
		/// Converts the specified bytes to a Guid. Byte order is considered.
		/// </summary>
		/// <param name="byteOrder">The byte order to use during the conversion.</param>
		/// <param name="bytes">The source byte array from which the bytes will be taken for conversion.</param>
		/// <param name="srcOffset">The offset into the source array where the bytes will be taken.</param>
		/// <param name="numElements">The number of elements that should be returned in the final int array.</param>
		/// <returns></returns>
		public static int[] ToInt32Array(ByteOrder byteOrder, byte[] bytes, int srcOffset, int numElements)
		{
			if (bytes == null)
			{
				throw new ArgumentNullException("bytes");
			}

			int[] values = new int[numElements];
			
			// this will consume 4 * N bytes
			for (int i = 0; i < numElements; i++)
			{
				values[i] = SocketUtilities.ToInt32(byteOrder, bytes, srcOffset + 4 * i);
			}

			return values;
		}
Exemple #23
0
 internal uint ReadUInt32(ByteOrder bo)
 {
     Contract.Requires(ReadBytesLeft >= sizeof(int));
     uint result;
     if (BitConverter.IsLittleEndian == (bo == ByteOrder.LSB))
     {
         result = BitConverter.ToUInt32(_buf, ReadPosition);
         ReadPosition += 4;
     }
     else
     {
         _workspace[3] = _buf[ReadPosition++];
         _workspace[2] = _buf[ReadPosition++];
         _workspace[1] = _buf[ReadPosition++];
         _workspace[0] = _buf[ReadPosition++];
         result = BitConverter.ToUInt32(_workspace, 0);
     }
     return result;
 }
		/// <summary>
		/// Returns a byte array containing 4 * N bytes where N is the number of elements in the integer array.
		/// </summary>
		/// <param name="byteOrder">The byte order to use during the conversion.</param>
		/// <param name="values">The array of integers that will be used to create the byte array.</param>
		/// <returns></returns>
		public static byte[] GetBytes(ByteOrder byteOrder, int[] values)
		{
			if (values == null)
			{
				throw new ArgumentNullException("values");
			}

			int offset = 0;
			byte[] resultBuffer = new byte[4 * values.Length];

			foreach (int value in values)
			{
				int length = value;

				if (byteOrder == ByteOrder.BigEndian)
				{
					length = IPAddress.HostToNetworkOrder(length);
				}
				byte[] buffer = BitConverter.GetBytes(length);
				Buffer.BlockCopy(buffer, 0, resultBuffer, offset, buffer.Length);
				offset += buffer.Length;
			}
			
			return resultBuffer;
		}