fix: Fix nullability warning in String.ToByteArray

This commit is contained in:
Michael Croes
2023-07-27 00:16:40 +02:00
parent 4aca9e4e53
commit 71f7f8b400

View File

@@ -12,13 +12,15 @@
/// <param name="reservedLength">The amount of bytes reserved for the <paramref name="value"/> in the PLC.</param> /// <param name="reservedLength">The amount of bytes reserved for the <paramref name="value"/> in the PLC.</param>
public static byte[] ToByteArray(string value, int reservedLength) public static byte[] ToByteArray(string value, int reservedLength)
{ {
var length = value?.Length;
if (length > reservedLength) length = reservedLength;
var bytes = new byte[reservedLength]; var bytes = new byte[reservedLength];
if (value == null) return bytes;
if (length == null || length == 0) return bytes; var length = value.Length;
if (length == 0) return bytes;
System.Text.Encoding.ASCII.GetBytes(value, 0, length.Value, bytes, 0); if (length > reservedLength) length = reservedLength;
System.Text.Encoding.ASCII.GetBytes(value, 0, length, bytes, 0);
return bytes; return bytes;
} }