diff --git a/S7.Net/Types/String.cs b/S7.Net/Types/String.cs index 3917635..b0ccc19 100644 --- a/S7.Net/Types/String.cs +++ b/S7.Net/Types/String.cs @@ -12,13 +12,15 @@ /// The amount of bytes reserved for the in the PLC. public static byte[] ToByteArray(string value, int reservedLength) { - var length = value?.Length; - if (length > reservedLength) length = 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; }