2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.regoheatpump.internal.rego6xx;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
18 * The {@link ValueConverter} is responsible for converting various rego 6xx specific data types.
20 * @author Boris Krivonog - Initial contribution
23 class ValueConverter {
24 public static byte[] shortToSevenBitFormat(short value) {
25 byte b1 = (byte) ((value & 0xC000) >> 14);
26 byte b2 = (byte) ((value & 0x3F80) >> 7);
27 byte b3 = (byte) (value & 0x007F);
29 return new byte[] { b1, b2, b3 };
32 public static short sevenBitFormatToShort(byte[] buffer, int offset) {
33 return (short) (buffer[offset] << 14 | buffer[offset + 1] << 7 | buffer[offset + 2]);
36 public static byte arrayToByte(byte[] buffer, int offset) {
37 return (byte) (buffer[offset] << 4 | buffer[offset + 1]);
40 public static String stringFromBytes(byte[] buffer, int offset, int charCount) {
41 StringBuilder builder = new StringBuilder(charCount);
43 int length = offset + charCount * 2;
44 for (int i = offset; i < length; i += 2) {
45 builder.append((char) arrayToByte(buffer, i));
48 return builder.toString();