]> git.basschouten.com Git - openhab-addons.git/blob
e9b9658a75757099add23193b84804e49ef527a2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.regoheatpump.internal.rego6xx;
14
15 /**
16  * The {@link ValueConverter} is responsible for converting various rego 6xx specific data types.
17  *
18  * @author Boris Krivonog - Initial contribution
19  */
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);
25
26         return new byte[] { b1, b2, b3 };
27     }
28
29     public static short sevenBitFormatToShort(byte[] buffer, int offset) {
30         return (short) (buffer[offset] << 14 | buffer[offset + 1] << 7 | buffer[offset + 2]);
31     }
32
33     public static byte arrayToByte(byte[] buffer, int offset) {
34         return (byte) (buffer[offset] << 4 | buffer[offset + 1]);
35     }
36
37     public static String stringFromBytes(byte[] buffer, int offset, int charCount) {
38         StringBuilder builder = new StringBuilder(charCount);
39
40         int length = offset + charCount * 2;
41         for (int i = offset; i < length; i += 2) {
42             builder.append((char) arrayToByte(buffer, i));
43         }
44
45         return builder.toString();
46     }
47 }