From 71f7f8b400075d0783e37b00a6e936d5499528e1 Mon Sep 17 00:00:00 2001 From: Michael Croes Date: Thu, 27 Jul 2023 00:16:40 +0200 Subject: [PATCH] fix: Fix nullability warning in String.ToByteArray --- S7.Net/Types/String.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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; }