/// <summary> /// Converts the text to decimal. /// </summary> private void ConvertDec() { if (SyntaxChecker.CanParseHex(this.DataType, this.Text)) { this.Text = Conversions.ParseHexStringAsPrimitiveString(this.DataType, this.Text); } this.SwitchDec(); }
/// <summary> /// Invoked when the convert to decimal menu item is clicked. /// </summary> /// <param name="sender">Sending object.</param> /// <param name="e">Event args.</param> private void ConvertToDecMenuItemClick(Object sender, EventArgs e) { if (SyntaxChecker.CanParseHex(this.ElementType, this.Text)) { this.Text = Conversions.ParseHexStringAsPrimitiveString(this.ElementType, this.Text); } this.IsHex = false; }
/// <summary> /// Hex string to an Int32. /// </summary> /// <param name="value">Value to be converted.</param> /// <param name="targetType">Type to convert to.</param> /// <param name="parameter">Optional conversion parameter.</param> /// <param name="culture">Globalization info.</param> /// <returns>An Int32. If conversion cannot take place, returns 0.</returns> public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture) { if (value != null) { if (SyntaxChecker.CanParseHex(targetType, value.ToString())) { return(Conversions.ParseHexStringAsPrimitive(targetType, value.ToString())); } } return(0); }
/// <summary> /// Converts a string to the corresponding value type. /// </summary> /// <param name="context">Type descriptor context.</param> /// <param name="culture">Globalization info.</param> /// <param name="value">The value being converted.</param> /// <returns>The converted value.</returns> public override Object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) { if (value is String) { IEnumerable <String> offsetStrings = (value as String).Split(',').Select(offset => offset.Trim()); if (offsetStrings.All(offset => SyntaxChecker.CanParseHex(DataType.Int32, offset))) { return(offsetStrings.Select(offset => (Int32)Conversions.ParseHexStringAsPrimitive(DataType.Int32, offset)).ToArray()); } } // Return an invalid object, such that it gets rejected by the property updater -- Note: This exception is not thrown return(new ArgumentException()); }
/// <summary> /// Initializes a new instance of the <see cref="AddressItem" /> class. /// </summary> /// <param name="baseAddress">The base address. This will be added as an offset from the resolved base identifier.</param> /// <param name="dataType">The data type of the value at this address.</param> /// <param name="description">The description of this address.</param> /// <param name="baseIdentifier">The identifier for the base address of this object.</param> /// <param name="offsets">The pointer offsets of this address item.</param> /// <param name="isValueHex">A value indicating whether the value at this address should be displayed as hex.</param> /// <param name="value">The value at this address. If none provided, it will be figured out later. Used here to allow immediate view updates upon creation.</param> public AddressItem( DataType dataType, String description = "New Address", Boolean isValueHex = false, Object value = null) : base(description) { // Bypass setters to avoid running setter code this.dataType = dataType; this.isValueHex = isValueHex; if (!this.isValueHex && SyntaxChecker.CanParseValue(dataType, value?.ToString())) { this.addressValue = value; } else if (this.isValueHex && SyntaxChecker.CanParseHex(dataType, value?.ToString())) { this.addressValue = value; } }
/// <summary> /// Determines if the current text is valid for the current data type. /// </summary> private void UpdateValidity() { if (this.IsHex) { if (SyntaxChecker.CanParseHex(this.ElementType, this.Text)) { this.IsTextValid = true; return; } } else { if (SyntaxChecker.CanParseValue(this.ElementType, this.Text)) { this.IsTextValid = true; return; } } this.IsTextValid = false; return; }
/// <summary> /// Converts an address string to the corresponding value. /// </summary> /// <param name="context">Type descriptor context.</param> /// <param name="culture">Globalization info.</param> /// <param name="value">The value being converted.</param> /// <returns>The converted value.</returns> public override Object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) { DataType dataType = null; Boolean isHex = false; if (typeof(AddressItem).IsAssignableFrom(context.Instance.GetType())) { dataType = (context.Instance as AddressItem)?.DataType; isHex = (context.Instance as AddressItem).IsValueHex; } if (dataType == (DataType)null || !value.GetType().IsAssignableFrom(typeof(String))) { return(base.ConvertFrom(context, culture, value)); } if (!(isHex ? SyntaxChecker.CanParseHex(dataType, value as String) : SyntaxChecker.CanParseValue(dataType, value as String))) { return(base.ConvertFrom(context, culture, value)); } return(isHex ? Conversions.ParseHexStringAsPrimitive(dataType, value as String) : Conversions.ParsePrimitiveStringAsPrimitive(dataType, value as String)); }