public GenericQbItem(string name, QbKey value, Type editType, bool readOnly, bool useQbItemType, QbItemType qbType, string sourceProperty) : this(name, editType, readOnly, useQbItemType, qbType, sourceProperty) { _value = value.ToString(); _type = value.GetType(); _typeNumeric = true; _qbKey = value; this.ConvertTo(editType); }
public GenericQbItem(string name, QbKey value, Type editType, bool readOnly, bool useQbItemType, QbItemType qbType, string sourceProperty) : this(name, editType, readOnly, useQbItemType, qbType, sourceProperty) { _value = value.ToString(); _type = value.GetType(); _typeNumeric = true; _qbKey = value; this.ConvertTo(editType); }
/// <summary> /// Convert text representations /// </summary> /// <param name="text"></param> /// <param name="toType">Type to convert to</param> /// <returns></returns> private string convert(Type toType) { byte[] b = null; float f; uint u; int i; string result = _value; if (_currType == null) { _currType = _type; } if (_currType == toType) { return(_value); } if (!this.CanConvertTo(toType)) { return(_value); } //if QBKey then swap between Hex and String if (_type == typeof(QbKey)) { QbKey qb = QbKey.Create(_value); if (_currType == typeof(byte[]) && toType == typeof(string)) { if (_qbKey == qb) { return(_qbKey.ToString()); } else { return(qb.ToString()); } } else if (_currType == typeof(string) && toType == typeof(byte[])) { if (_value.Trim().Length != 0) { _qbKey = QbKey.Create(_value); } return(_qbKey.Crc.ToString("X").PadLeft(8, '0')); } else { return(_value); } } //get the data into a byte array if (_currType == typeof(float)) { if (float.TryParse(_value, out f)) { b = BitConverter.GetBytes(f); } } else if (_currType == typeof(uint)) { if (uint.TryParse(_value, out u)) { b = BitConverter.GetBytes(u); } } else if (_currType == typeof(int)) { if (int.TryParse(_value, out i)) { b = BitConverter.GetBytes(i); } } else if (_currType == typeof(byte[])) { if (_value.Length > 2) { b = new byte[_value.Length / 2]; for (int c = 0; c < _value.Length; c += 2) { b[c / 2] = byte.Parse(_value.Substring(c, 2), System.Globalization.NumberStyles.HexNumber); } if (_typeNumeric && BitConverter.IsLittleEndian) { Array.Reverse(b); } } } else if (_currType == typeof(string)) { b = Encoding.Default.GetBytes(_value); } //else // throw new ArgumentOutOfRangeException(string.Format("{0} is not supported", _currType.FullName)); if (b != null) { //convert the data to the new type if (toType == typeof(float)) { f = BitConverter.ToSingle(b, 0); result = f.ToString(); } else if (toType == typeof(uint)) { u = BitConverter.ToUInt32(b, 0); result = u.ToString(); } else if (toType == typeof(int)) { i = BitConverter.ToInt32(b, 0); result = i.ToString(); } else if (toType == typeof(byte[])) { StringBuilder sb = new StringBuilder(); if (_typeNumeric && BitConverter.IsLittleEndian) { Array.Reverse(b); } foreach (byte x in b) { sb.Append(x.ToString("X").PadLeft(2, '0')); } result = sb.ToString(); } else if (toType == typeof(string)) { result = Encoding.Default.GetString(b); } //else // throw new ArgumentOutOfRangeException(string.Format("{0} is not supported", toType.FullName)); } return(result); }