private void GenerateMachineCodeForDataFields(IList <string> dataFields, DataFieldTypes dataType, ParserState state) { for (int j = 0; j < dataFields.Count; j++) { string field = dataFields[j]; if (field == string.Empty) { continue; } if (field.IndexOf('\"') == 0) { for (int i = 1; i < field.Length; i++) { char c = field[i]; switch (dataType) { case DataFieldTypes.Int8: state.Code.Add((byte)c); break; case DataFieldTypes.Int16: state.Code.Add((byte)(c & 0x00ff)); state.Code.Add((byte)((ushort)(c & 0xff00) >> 8)); break; } } } else { string valField = field.Trim(); if (valField == string.Empty) { continue; } uint val; if (valField.Substring(0, 1) == "$") { val = Convert.ToUInt32(valField.Substring(1, valField.Length - 1), 16); } else if (valField.Length >= 2 && valField.Substring(0, 2) == "0x") { val = Convert.ToUInt32(valField.Substring(2, valField.Length - 2), 16); } else if (uint.TryParse(valField, out val)) { val = uint.Parse(valField); // do this twice? just to have something in the if statement... } else { state.DataFields.Add((ushort)state.Code.Count, valField); } switch (dataType) { case DataFieldTypes.Int8: if (val > byte.MaxValue) { throw new Exception($"Included byte value '{field}' cannot be expressed in an 8-bit value"); } state.Code.Add((byte)val); break; case DataFieldTypes.Int16: if (val > ushort.MaxValue) { throw new Exception($"Included ushort value '{field}' cannot be expressed in a 16-bit value"); } state.Code.Add((byte)((ushort)val & 0x00ff)); state.Code.Add((byte)((ushort)(val & 0xff00) >> 8)); break; } } } }
private void GenerateMachineCodeForDataFields(IList<string> dataFields, DataFieldTypes dataType, ParserState state) { for (int j = 0; j < dataFields.Count; j++) { string field = dataFields[j]; if (field == string.Empty) continue; if (field.IndexOf('\"') == 0) { for (int i = 1; i < field.Length; i++) { char c = field[i]; switch (dataType) { case DataFieldTypes.Int8: state.Code.Add((byte)c); break; case DataFieldTypes.Int16: state.Code.Add((byte)(c & 0x00ff)); state.Code.Add((byte)((ushort)(c & 0xff00) >> 8)); break; } } } else { string valField = field.Trim(); if (valField == string.Empty) continue; uint val; if (valField.Substring(0, 1) == "$") { val = Convert.ToUInt32(valField.Substring(1, valField.Length - 1), 16); } else if (valField.Length >= 2 && valField.Substring(0, 2) == "0x") { val = Convert.ToUInt32(valField.Substring(2, valField.Length - 2), 16); } else if (uint.TryParse(valField, out val)) { val = uint.Parse(valField); // do this twice? just to have something in the if statement... } else { state.DataFields.Add((ushort)state.Code.Count, valField); } switch (dataType) { case DataFieldTypes.Int8: if (val > byte.MaxValue) throw new Exception($"Included byte value '{field}' cannot be expressed in an 8-bit value"); state.Code.Add((byte)val); break; case DataFieldTypes.Int16: if (val > ushort.MaxValue) throw new Exception($"Included ushort value '{field}' cannot be expressed in a 16-bit value"); state.Code.Add((byte)((ushort)val & 0x00ff)); state.Code.Add((byte)((ushort)(val & 0xff00) >> 8)); break; } } } }