mirror of
https://github.com/evopro-ag/Sharp7Reactive.git
synced 2025-12-15 11:22:52 +00:00
Fix string to byte array encoding
This commit is contained in:
@@ -23,12 +23,31 @@ internal class WriteToBuffer : ConverterTestBase
|
||||
|
||||
public static IEnumerable<ConverterTestCase> GetAdditinalWriteTestCases()
|
||||
{
|
||||
yield return new ConverterTestCase("a", "DB0.Byte80.3", [0x61, 0x00, 0x00]); // short string
|
||||
yield return new ConverterTestCase("abc", "DB0.Byte80.3", [0x61, 0x62, 0x63]); // matching string
|
||||
yield return new ConverterTestCase("abcxx", "DB0.Byte80.3", [0x61, 0x62, 0x63]); // long string
|
||||
|
||||
yield return new ConverterTestCase("a", "DB0.string0.3", [0x03, 0x01, 0x61, 0x00, 0x00]); // short string
|
||||
yield return new ConverterTestCase("abc", "DB0.string0.3", [0x03, 0x03, 0x61, 0x62, 0x63]); // matching string
|
||||
yield return new ConverterTestCase("abcxx", "DB0.string0.3", [0x03, 0x03, 0x61, 0x62, 0x63]); // long string
|
||||
|
||||
yield return new ConverterTestCase("a", "DB0.wstring0.3", [0x00, 0x03, 0x00, 0x01, 0x00, 0x61, 0x00, 0x00, 0x00, 0x00]); // short string
|
||||
yield return new ConverterTestCase("abc", "DB0.wstring0.3", [0x00, 0x03, 0x00, 0x03, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63]); // matching string
|
||||
yield return new ConverterTestCase("abcxx", "DB0.wstring0.3", [0x00, 0x03, 0x00, 0x03, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63]); // long string
|
||||
|
||||
|
||||
yield return new ConverterTestCase("aaaaBCDE", "DB0.string0.4", [0x04, 0x04, 0x61, 0x61, 0x61, 0x61]); // Length in address exceeds PLC string length
|
||||
yield return new ConverterTestCase("aaaaBCDE", "DB0.WString0.4", [0x00, 0x04, 0x00, 0x04, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61]); // Length in address exceeds PLC string length
|
||||
yield return new ConverterTestCase("aaaaBCDE", "DB0.DBB0.4", [0x61, 0x61, 0x61, 0x61]); // Length in address exceeds PLC array length
|
||||
yield return new ConverterTestCase("\ud83d\udc69\ud83c\udffd\u200d\ud83d\ude80", "DB0.WString0.2", [0x00, 0x02, 0x00, 0x02, 0xD8, 0x3D, 0xDC, 0x69]); // Length in address exceeds PLC string length, multi char unicode point
|
||||
yield return new ConverterTestCase("\ud83d\udc69\ud83c\udffd\u200d\ud83d\ude80", "DB0.String0.2", [0x02, 0x02, 0x3F, 0x3F]); // Length in address exceeds PLC string length, multi char unicode point
|
||||
yield return new ConverterTestCase("\ud83d\udc69\ud83c\udffd\u200d\ud83d\ude80", "DB0.DBB0.4", [0x3F, 0x3F, 0x3F, 0x3F]); // Length in address exceeds PLC string length, multi char unicode point
|
||||
|
||||
// Length in address exceeds PLC string length, multi char unicode point
|
||||
yield return new ConverterTestCase("\ud83d\udc69\ud83c\udffd\u200d\ud83d\ude80", "DB0.WString0.2", [0x00, 0x02, 0x00, 0x02, 0xD8, 0x3D, 0xDC, 0x69]);
|
||||
|
||||
// Length in address exceeds PLC string length, multi char unicode point
|
||||
yield return new ConverterTestCase("\ud83d\udc69\ud83c\udffd\u200d\ud83d\ude80", "DB0.String0.2", [0x02, 0x02, 0x3F, 0x3F]);
|
||||
|
||||
// Length in address exceeds PLC string length, multi char unicode point
|
||||
yield return new ConverterTestCase("\ud83d\udc69\ud83c\udffd\u200d\ud83d\ude80", "DB0.DBB0.4", [0x3F, 0x3F, 0x3F, 0x3F]);
|
||||
}
|
||||
|
||||
[TestCase(18, "DB0.DInt12", 3)]
|
||||
|
||||
@@ -54,7 +54,9 @@ internal static class ValueConverter
|
||||
EncodeWString(data);
|
||||
return;
|
||||
case DbType.Byte:
|
||||
Encoding.ASCII.GetBytes(stringValue.AsSpan(0, address.Length), data);
|
||||
|
||||
var readOnlySpan = stringValue.AsSpan(0, Math.Min(address.Length, stringValue.Length));
|
||||
Encoding.ASCII.GetBytes(readOnlySpan, data);
|
||||
return;
|
||||
default:
|
||||
throw new DataTypeMissmatchException($"Cannot write string to {address.Type}", typeof(string), address);
|
||||
|
||||
Reference in New Issue
Block a user