/// <summary> /// Converts four bytes from a byte array to a float. /// </summary> /// <param name="bytes">Bytes to be converted.</param> /// <param name="startIndex">Index of the starting byte.</param> static public float ToFloat(byte[] bytes, int startIndex) { byte[] subBytes = HHelper.SubArray(bytes, startIndex, 4); if (BitConverter.IsLittleEndian) { Array.Reverse(subBytes); } return(BitConverter.ToSingle(subBytes, 0)); }
/// <summary> /// Converts a byte array to a string, using BigEndianUnicode. /// </summary> /// <param name="bytes">Bytes to be converted.</param> /// <param name="startIndex">Index of the starting byte.</param> /// <param name="length">Number of bytes to convert.</param> static public string ToBigEndianUnicodeString(byte[] bytes, int startIndex, int length) { byte[] subBytes = HHelper.SubArray(bytes, startIndex, length); //subBytes = Helper.ConcatinateArray(new byte[] { 0xFE, 0xFF }, subBytes); //if (BitConverter.IsLittleEndian) // Array.Reverse(subBytes); string text = Encoding.BigEndianUnicode.GetString(subBytes); // UTF-16 BigEndian to string. return(text); }
/// <summary> /// Converts four bytes from a byte array to a int32. Using BigEndian. /// </summary> /// <param name="bytes">Bytes to be converted.</param> /// <param name="startIndex">Index of the starting byte.</param> static public int ToInt32(byte[] bytes, int startIndex) { if (BitConverter.IsLittleEndian) { bytes = HHelper.SubArray(bytes, startIndex, 4); Array.Reverse(bytes); startIndex = 0; } return(BitConverter.ToInt32(bytes, startIndex)); }