From d808ea5eb6944c782f452f5f3ed38a1ec9db22cc Mon Sep 17 00:00:00 2001 From: diego Date: Thu, 16 Jun 2022 17:21:23 +0200 Subject: [PATCH 1/3] Give the option of changing the Encoding used to serialize and deserialize S7String, instead of always using Encoding.ASCII --- S7.Net/Types/S7String.cs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/S7.Net/Types/S7String.cs b/S7.Net/Types/S7String.cs index 5bc383ef..671c8e65 100644 --- a/S7.Net/Types/S7String.cs +++ b/S7.Net/Types/S7String.cs @@ -9,6 +9,11 @@ namespace S7.Net.Types /// public static class S7String { + /// + /// The Encoding used when serializing and deserializing S7String (Encoding.ASCII by default) + /// + public static Encoding StringEncoding { get; set; } + /// /// Converts S7 bytes to a string /// @@ -30,7 +35,8 @@ public static string FromByteArray(byte[] bytes) try { - return Encoding.ASCII.GetString(bytes, 2, length); + if (StringEncoding == null) StringEncoding = Encoding.ASCII; + return StringEncoding.GetString(bytes, 2, length); } catch (Exception e) { @@ -38,7 +44,6 @@ public static string FromByteArray(byte[] bytes) $"Failed to parse {VarType.S7String} from data. Following fields were read: size: '{size}', actual length: '{length}', total number of bytes (including header): '{bytes.Length}'.", e); } - } /// @@ -54,9 +59,10 @@ public static byte[] ToByteArray(string value, int reservedLength) throw new ArgumentNullException(nameof(value)); } - if (reservedLength > 254) throw new ArgumentException($"The maximum string length supported is 254."); - - var bytes = Encoding.ASCII.GetBytes(value); + if (reservedLength > 254) throw new ArgumentException($"The maximum string length supported is 254."); + + if (StringEncoding == null) StringEncoding = Encoding.ASCII; + var bytes = StringEncoding.GetBytes(value); if (bytes.Length > reservedLength) throw new ArgumentException($"The provided string length ({bytes.Length} is larger than the specified reserved length ({reservedLength})."); var buffer = new byte[2 + reservedLength]; @@ -65,5 +71,13 @@ public static byte[] ToByteArray(string value, int reservedLength) buffer[1] = (byte)bytes.Length; return buffer; } + + /// + /// Initializes default static properties + /// + static S7String() + { + StringEncoding = Encoding.ASCII; + } } } From 2ecd2c6b4936fda6d7f5d7e2cd9d947a3c9dc6ab Mon Sep 17 00:00:00 2001 From: diego Date: Fri, 17 Jun 2022 11:08:09 +0200 Subject: [PATCH 2/3] Changes following code review --- S7.Net/Types/S7String.cs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/S7.Net/Types/S7String.cs b/S7.Net/Types/S7String.cs index 671c8e65..8e67db61 100644 --- a/S7.Net/Types/S7String.cs +++ b/S7.Net/Types/S7String.cs @@ -8,11 +8,18 @@ namespace S7.Net.Types /// An S7 String has a preceeding 2 byte header containing its capacity and length /// public static class S7String - { - /// - /// The Encoding used when serializing and deserializing S7String (Encoding.ASCII by default) - /// - public static Encoding StringEncoding { get; set; } + { + private static Encoding stringEncoding; + + /// + /// The Encoding used when serializing and deserializing S7String (Encoding.ASCII by default) + /// + /// StringEncoding must not be null + public static Encoding StringEncoding + { + get => stringEncoding ??= Encoding.ASCII; + set => stringEncoding = value ?? throw new ArgumentNullException(nameof(StringEncoding)); + } /// /// Converts S7 bytes to a string @@ -35,7 +42,6 @@ public static string FromByteArray(byte[] bytes) try { - if (StringEncoding == null) StringEncoding = Encoding.ASCII; return StringEncoding.GetString(bytes, 2, length); } catch (Exception e) @@ -59,9 +65,8 @@ public static byte[] ToByteArray(string value, int reservedLength) throw new ArgumentNullException(nameof(value)); } - if (reservedLength > 254) throw new ArgumentException($"The maximum string length supported is 254."); - - if (StringEncoding == null) StringEncoding = Encoding.ASCII; + if (reservedLength > 254) throw new ArgumentException($"The maximum string length supported is 254."); + var bytes = StringEncoding.GetBytes(value); if (bytes.Length > reservedLength) throw new ArgumentException($"The provided string length ({bytes.Length} is larger than the specified reserved length ({reservedLength})."); @@ -71,13 +76,5 @@ public static byte[] ToByteArray(string value, int reservedLength) buffer[1] = (byte)bytes.Length; return buffer; } - - /// - /// Initializes default static properties - /// - static S7String() - { - StringEncoding = Encoding.ASCII; - } } } From ec554ddb59c3011b9631e36641f0e33a6d891710 Mon Sep 17 00:00:00 2001 From: diego Date: Fri, 17 Jun 2022 11:23:58 +0200 Subject: [PATCH 3/3] Better stringEncoding initialization, as per code review comments --- S7.Net/Types/S7String.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/S7.Net/Types/S7String.cs b/S7.Net/Types/S7String.cs index 8e67db61..46c4808a 100644 --- a/S7.Net/Types/S7String.cs +++ b/S7.Net/Types/S7String.cs @@ -9,7 +9,7 @@ namespace S7.Net.Types /// public static class S7String { - private static Encoding stringEncoding; + private static Encoding stringEncoding = Encoding.ASCII; /// /// The Encoding used when serializing and deserializing S7String (Encoding.ASCII by default) @@ -17,7 +17,7 @@ public static class S7String /// StringEncoding must not be null public static Encoding StringEncoding { - get => stringEncoding ??= Encoding.ASCII; + get => stringEncoding; set => stringEncoding = value ?? throw new ArgumentNullException(nameof(StringEncoding)); }