/// <summary> /// 从float转换到TwFix32 /// </summary> /// <param name="floatValue"></param> /// <returns></returns> public static TwFix32 FloatToFix32(float floatValue) { TwFix32 fix32 = new TwFix32(); Int32 value = (Int32)(floatValue * 65536.0 + 0.5); fix32.Whole = (Int16)(value >> 16); fix32.Frac = (UInt16)(value & 0x0000ffffL); return(fix32); }
/// <summary> /// 将形如XXXX.XXXX的字符串转换为fix32数据类型 /// </summary> /// <param name="itemValue">解析的字符串</param> /// <param name="fix32">解析完成后初始化的对象</param> /// <returns>解析成功返回true,否则返回false</returns> public static bool Parse(string itemValue, ref TwFix32 fix32) { ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); // log4net.Config.XmlConfigurator.Configure(new FileInfo(Config.GetConfigDir() + ConstCommon.logConfigFileName)); try { // 1:按照.对字符串进行拆分 char[] separator = { '.' }; string[] results = itemValue.Split(separator, StringSplitOptions.RemoveEmptyEntries); fix32.Whole = short.MinValue; fix32.Frac = ushort.MinValue; //2:如果字符串非空 if (results.Length > 0) { //2.3.1:给fix32整数部分进行赋值 fix32.Whole = short.Parse(results[0]); //2.3.1:给fix32小数部分进行赋值 if (results.Length == 2) { fix32.Frac = (ushort)(double.Parse(results[1])); } else { fix32.Frac = 0; } } return(true); } #region 异常处理 catch (ArgumentNullException e) { logger.Error("argument is null in TwFix32.Parse function " + e.Message); return(false); } catch (FormatException e) { logger.Error("FormatException is wrong in TwFix32.Parse function " + e.Message); return(false); } catch (OverflowException e) { logger.Error("OverflowException occured in TwFix32.Parse function " + e.Message); return(false); } catch (Exception e) { logger.Error("Exception occured in TwFix32.Parse function " + e.Message); return(false); } #endregion }
/// <summary> /// 将形如XXXX.XXXX:XXXX.XXXX:XXXX.XXXX:XXXX.XXXX的字符串转换为frame数据类型 /// </summary> /// <param name="itemValue">解析的字符串</param> /// <param name="fix32">解析完成后初始化的对象</param> /// <returns>解析成功返回true,否则返回false</returns> public static bool Parse(string itemValue, ref TwFrame twFrame) { char[] separator = { ':' }; string[] results = itemValue.Split(separator, StringSplitOptions.RemoveEmptyEntries); //字符串数组不为4个 if (results.Length != 4) { return(false); } if (!(TwFix32.Parse(results[0], ref twFrame.Left) && TwFix32.Parse(results[1], ref twFrame.Top) && TwFix32.Parse(results[2], ref twFrame.Right) && TwFix32.Parse(results[3], ref twFrame.Bottom))) { return(false); } else { return(true); } }