2 * Copyright (c) 2010-2022 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;
16 * The {@link ValueConverter} is responsible for converting various rego 6xx specific data types.
18 * @author Boris Krivonog - Initial contribution
20 class ValueConverter {
21 public static byte[] shortToSevenBitFormat(short value) {
22 byte b1 = (byte) ((value & 0xC000) >> 14);
23 byte b2 = (byte) ((value & 0x3F80) >> 7);
24 byte b3 = (byte) (value & 0x007F);
26 return new byte[] { b1, b2, b3 };
29 public static short sevenBitFormatToShort(byte[] buffer, int offset) {
30 return (short) (buffer[offset] << 14 | buffer[offset + 1] << 7 | buffer[offset + 2]);
33 public static byte arrayToByte(byte[] buffer, int offset) {
34 return (byte) (buffer[offset] << 4 | buffer[offset + 1]);
37 public static String stringFromBytes(byte[] buffer, int offset, int charCount) {
38 StringBuilder builder = new StringBuilder(charCount);
40 int length = offset + charCount * 2;
41 for (int i = offset; i < length; i += 2) {
42 builder.append((char) arrayToByte(buffer, i));
45 return builder.toString();