diff --git a/bundles/org.openhab.binding.elroconnects/src/main/java/org/openhab/binding/elroconnects/internal/util/ElroConnectsUtil.java b/bundles/org.openhab.binding.elroconnects/src/main/java/org/openhab/binding/elroconnects/internal/util/ElroConnectsUtil.java index 7fe252eef21..8c6207a7f8b 100644 --- a/bundles/org.openhab.binding.elroconnects/src/main/java/org/openhab/binding/elroconnects/internal/util/ElroConnectsUtil.java +++ b/bundles/org.openhab.binding.elroconnects/src/main/java/org/openhab/binding/elroconnects/internal/util/ElroConnectsUtil.java @@ -46,8 +46,10 @@ public final class ElroConnectsUtil { */ public static String encode(String input, int length) { byte[] bytes = input.getBytes(StandardCharsets.UTF_8); - String content = "@".repeat(length - bytes.length) + new String(bytes, StandardCharsets.UTF_8) + "$"; - bytes = content.getBytes(StandardCharsets.UTF_8); + String content = "@".repeat((length > bytes.length) ? (length - bytes.length) : 0) + + new String(bytes, StandardCharsets.UTF_8); + bytes = Arrays.copyOf(content.getBytes(StandardCharsets.UTF_8), length); + bytes[length] = (byte) "$".charAt(0); return HexUtils.bytesToHex(bytes); }