123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- namespace Aitex.Core.Util
- {
- public static class Converter
- {
- ///// <summary>
- ///// Unit converter from physical to logical in linear formula
- ///// </summary>
- ///// <returns></returns>
- public static double Phy2Logic(double physicalValue, double logicalMin, double logicalMax, double physicalMin, double physicalMax)
- {
- var ret = (logicalMax - logicalMin) * (physicalValue - physicalMin) / (physicalMax - physicalMin) + logicalMin;
- //if (ret < logicalMin) ret = logicalMin;
- //if (ret > logicalMax) ret = logicalMax;
- if (double.IsNaN(ret)) throw new DivideByZeroException("Phy2Logic除0操作");
- return ret;
- }
- ///// <summary>
- ///// Unit converter from logical to physical in linear formula
- ///// </summary>
- ///// <returns></returns>
- public static double Logic2Phy(double logicalValue, double logicalMin, double logicalMax, double physicalMin, double physicalMax)
- {
- var ret = (physicalMax - physicalMin) * (logicalValue - logicalMin) / (logicalMax - logicalMin) + physicalMin;
- if (ret < physicalMin) ret = physicalMin;
- if (ret > physicalMax) ret = physicalMax;
- if (double.IsNaN(ret))
- throw new DivideByZeroException("Logic2Phy除0操作");
- return ret;
- }
- /// <summary>
- /// 只按占用的字节转换,不检查范围
- /// </summary>
- /// <param name="high"></param>
- /// <param name="low"></param>
- /// <returns></returns>
- public static float TwoUInt16ToFloat(UInt16 high, UInt16 low)
- {
- Int32 sum = (high << 16) + (low & 0xFFFF);
- byte[] bs = BitConverter.GetBytes(sum);
- return BitConverter.ToSingle(bs, 0);
- }
- public static int TwoInt16ToInt32(Int16 high, Int16 low)
- {
- UInt32 tmp = (ushort)low + (UInt32)(((ushort)high) << 16);
- return tmp > Int32.MaxValue ? (int)((~tmp + 1) * -1) : (int)tmp;////unsigned to signed
- }
-
- public static float TwoInt16ToFloat(Int16 high, Int16 low)
- {
- Int32 sum = (high << 16) + (low & 0xFFFF);
- byte[] bs = BitConverter.GetBytes(sum);
- return BitConverter.ToSingle(bs, 0);
- }
- }
- }
|