-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat request: SerializeAs Support IValueConverter #199
Comments
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));
}
}
} |
zh3305
pushed a commit
to zh3305/BinarySerializer
that referenced
this issue
Feb 17, 2023
using ``` [FieldOrder(0)] [SerializeAs(SerializedType.TerminatedString, StringTerminator = (char)0x20, ConverterType = typeof(HexStringToIntConvert))] public int DataAmount { get; set; } ``` Refs: jefffhaynes#199
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
The text was updated successfully, but these errors were encountered: