/// <summary> /// Changes the numeric type of an object and then calls a function to Format the Display String to correspond to the numeric Type /// </summary> public static void ChangeDecHexDisplayValuesSingleItem(DictItem item) { if (item.NumericType == NUM_TYPE.DEC) { item.NumericType = NUM_TYPE.HEX; } else if (item.NumericType == NUM_TYPE.HEX) { item.NumericType = NUM_TYPE.BIN; } else if (item.NumericType == NUM_TYPE.BIN) { item.NumericType = NUM_TYPE.DEC; } if (item.ValueDisplay != "" && item.Type.Substring(0, 3) != "STR") { FormatDisplayString(item); } }
public static void SetValueFromDisplayString(DictItem item) { if (item.Type == "STRING") { item.Value = Encoding.ASCII.GetBytes(item.ValueDisplay); return; } if (item.NumericType == NUM_TYPE.DEC) { var styles = NumberStyles.AllowThousands | NumberStyles.AllowLeadingSign; switch (item.Type) { case "SINT": item.Value = sbyte.Parse(item.ValueDisplay, styles, CultureInfo.CurrentCulture); break; case "USINT": item.Value = byte.Parse(item.ValueDisplay, styles, CultureInfo.CurrentCulture); break; case "INT": item.Value = short.Parse(item.ValueDisplay, styles, CultureInfo.CurrentCulture); break; case "UINT": item.Value = ushort.Parse(item.ValueDisplay, styles, CultureInfo.CurrentCulture); break; case "DINT": item.Value = int.Parse(item.ValueDisplay, styles, CultureInfo.CurrentCulture); break; case "UDINT": item.Value = uint.Parse(item.ValueDisplay, styles, CultureInfo.CurrentCulture); break; default: throw new NotImplementedException(); } } else if (item.NumericType == NUM_TYPE.HEX) { switch (item.Type) { case "SINT": item.Value = sbyte.Parse(Regex.Replace(item.ValueDisplay, @"\s+", ""), NumberStyles.HexNumber); break; case "USINT": item.Value = byte.Parse(Regex.Replace(item.ValueDisplay, @"\s+", ""), NumberStyles.HexNumber); break; case "INT": item.Value = short.Parse(Regex.Replace(item.ValueDisplay, @"\s+", ""), NumberStyles.HexNumber); break; case "UINT": item.Value = ushort.Parse(Regex.Replace(item.ValueDisplay, @"\s+", ""), NumberStyles.HexNumber); break; case "DINT": item.Value = int.Parse(Regex.Replace(item.ValueDisplay, @"\s+", ""), NumberStyles.HexNumber); break; case "UDINT": item.Value = uint.Parse(Regex.Replace(item.ValueDisplay, @"\s+", ""), NumberStyles.HexNumber); break; default: throw new NotImplementedException(); } } else if (item.NumericType == NUM_TYPE.BIN) { switch (item.Type) { case "SINT": item.Value = Convert.ToSByte(Regex.Replace(item.ValueDisplay, @"\s+", ""), 2); break; case "USINT": item.Value = Convert.ToByte(Regex.Replace(item.ValueDisplay, @"\s+", ""), 2); break; case "INT": item.Value = Convert.ToInt16(Regex.Replace(item.ValueDisplay, @"\s+", ""), 2); break; case "UINT": item.Value = Convert.ToUInt16(Regex.Replace(item.ValueDisplay, @"\s+", ""), 2); break; case "DINT": item.Value = Convert.ToInt32(Regex.Replace(item.ValueDisplay, @"\s+", ""), 2); break; case "UDINT": item.Value = Convert.ToUInt32(Regex.Replace(item.ValueDisplay, @"\s+", ""), 2); break; default: throw new NotImplementedException(); } } }
/// <summary> /// Formats the Display string of the given dictionary item /// depending if if it is decimal, binary or hexadecimal; or if it is a string /// </summary> /// <param name="item">item of type DictItem that needs the Display value to be formated</param> public static void FormatDisplayString(DictItem item) { if (item.Type == "STRING") { var len = (item.Value as byte[]).TakeWhile(b => b != 0).Count(); item.ValueDisplay = Encoding.ASCII.GetString(item.Value as byte[], 0, len); return; } if (item.NumericType == NUM_TYPE.DEC) { item.ValueDisplay = string.Format("{0:N0}", item.Value, CultureInfo.CurrentCulture); } else if (item.NumericType == NUM_TYPE.HEX) { switch (item.Type) { case "SINT": item.ValueDisplay = string.Format("{0:X2}", (sbyte)item.Value); break; case "USINT": item.ValueDisplay = string.Format("{0:X2}", (byte)item.Value); break; case "INT": item.ValueDisplay = string.Format("{0:X2} {1:X2}", (byte)((short)item.Value >> 8), (byte)((short)item.Value >> 0)); break; case "UINT": item.ValueDisplay = string.Format("{0:X2} {1:X2}", (byte)((ushort)item.Value >> 8), (byte)((ushort)item.Value >> 0)); break; case "DINT": item.ValueDisplay = string.Format("{0:X2} {1:X2} {2:X2} {3:X2}", (byte)((int)item.Value >> 24), (byte)((int)item.Value >> 16), (byte)((int)item.Value >> 8), (byte)((int)item.Value >> 0)); break; case "UDINT": item.ValueDisplay = string.Format("{0:X2} {1:X2} {2:X2} {3:X2}", (byte)((uint)item.Value >> 24), (byte)((uint)item.Value >> 16), (byte)((uint)item.Value >> 8), (byte)((uint)item.Value >> 0)); break; default: throw new NotImplementedException(); } } else if (item.NumericType == NUM_TYPE.BIN) { string temp_str; switch (item.Type) { case "SINT": temp_str = Convert.ToString(Convert.ToSByte(item.Value), 2).PadLeft(8, '0'); if (temp_str.Length <= 8) { item.ValueDisplay = Regex.Replace(temp_str, @".{4}", @"$0 "); } else { item.ValueDisplay = Regex.Replace(TruncateToEnd(temp_str, 8), @".{4}", @"$0 "); } break; case "USINT": item.ValueDisplay = Regex.Replace(Convert.ToString(Convert.ToByte(item.Value), 2).PadLeft(8, '0'), @".{4}", @"$0 "); temp_str = Convert.ToString(Convert.ToByte(item.Value), 2).PadLeft(8, '0'); if (temp_str.Length <= 8) { item.ValueDisplay = Regex.Replace(temp_str, @".{4}", @"$0 "); } else { item.ValueDisplay = Regex.Replace(TruncateToEnd(temp_str, 8), @".{4}", @"$0 "); } break; case "INT": item.ValueDisplay = Regex.Replace(Convert.ToString((short)item.Value, 2).PadLeft(16, '0'), @".{4}", @"$0 "); break; case "UINT": item.ValueDisplay = Regex.Replace(Convert.ToString((ushort)item.Value, 2).PadLeft(16, '0'), @".{4}", @"$0 "); break; case "DINT": item.ValueDisplay = Regex.Replace(Convert.ToString((int)item.Value, 2).PadLeft(32, '0'), @".{4}", @"$0 "); break; case "UDINT": item.ValueDisplay = Regex.Replace(Convert.ToString((uint)item.Value, 2).PadLeft(32, '0'), @".{4}", @"$0 "); break; default: throw new NotImplementedException(); } } }