Open zh3305 opened 1 year ago
Can you convert the hex to binary first? I think it would be easier to process then.
Can you convert the hex to binary first? I think it would be easier to process then.
I have created a small test
I want to change DataAmount,DistData.Data to int type
[TestClass]
public class Issue199Tests : TestBase
{
BinarySerializer serializer = new BinarySerializer();
[TestMethod]
public void Test()
{
var testdata = "A ED7 F13 F25 F26 EBC E82 E60 E43 DD9 DC3 DC4 ";
var stringToByte = StringToByte(testdata);
var outputChannel = serializer.Deserialize<OutputChannel>(stringToByte);
// TestContext.Out.WriteLine(JsonConvert.SerializeObject(outputChannel));
Assert.AreEqual(Convert.ToInt32(outputChannel.DataAmount, 16), outputChannel.DistDatas.Count);
Assert.AreEqual(Convert.ToInt32("ED7", 16), Convert.ToInt32(outputChannel.DistDatas[0].Data, 16));
}
public static byte[] StringToByte(string str)
{
byte[] bytes = new byte[str.Length];
for (int i = 0; i < str.Length; i++)
{
bytes[i] = (byte)str[i];
}
return bytes;
}
public class OutputChannel
{
[FieldOrder(0)]
[SerializeAs(SerializedType.TerminatedString, StringTerminator = (char)0x20)]
public string DataAmount { get; set; }
//数据列表
[FieldOrder(1)]
[FieldCount(nameof(DataAmount), ConverterType = typeof(StringLengtConvert))]
public List<DistData> DistDatas { get; set; }
}
public class DistData
{
[SerializeAs(SerializedType.TerminatedString, StringTerminator = (char)0x20)]
public string Data { get; set; }
}
class StringLengtConvert : IValueConverter
{
// Read
public object Convert(object value, object parameter, BinarySerializationContext context)
{
return (System.Convert.ToInt32((string)value, 16) + System.Convert.ToInt32(parameter));
}
//Write
public object ConvertBack(object value, object parameter, BinarySerializationContext context)
{
return (System.Convert.ToInt32(value) - System.Convert.ToInt32(parameter));
}
}
}
hello I'm trying to use this package to interface with Sick Lidars. I'm trying to read int data in a hexadecimal ASCII string
sourse
Normal running code:
I want to change string to int:
I looked at BinarySerialization.Graph.ValueGraph class's method Deserialize, when the code ConvertToFieldType (value). IValueConverter or Convet Format is not supported
127
76