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.fineoffsetweatherstation.internal;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
20 * @author Andreas Berger - Initial contribution
25 public static String toHexString(byte[] hex, int length, String delimiter) {
26 String[] hexadecimal = new String[length];
27 for (int i = 0; i < length; i++) {
28 hexadecimal[i] = String.format("%02X", hex[i]);
30 return String.join(delimiter, hexadecimal);
33 public static boolean validateChecksum(byte[] data, int sizeBytes) {
37 // | 1 byte size | 2 byte size
38 // | -----------------------------|--------------------
39 // | 0 - 0xff - header | 0 - 0xff - header
40 // | 1 - 0xff | 1 - 0xff
41 // | 2 - command | 2 - command
42 // | 3 - total size of response | 3 - size1
43 // | 4-X - data | 4 - size2
44 // | X+1 - checksum | 5-X - data
48 size = Utils.toUInt8(data[3]);
50 size = toUInt16(data, 3);
53 byte checksum = sum(data, 2, size);
54 return checksum == data[size + 1];
57 private static byte sum(byte[] data, int start, int end) {
59 for (var i = start; i <= end; i++) {
65 public static int toUInt8(byte data) {
66 return Byte.toUnsignedInt(data);
69 public static int toInt16(byte[] array, int start) {
70 int result = ((int) array[start]) << 24;
71 result |= Utils.toUInt8(array[start + 1]) << 16;
75 public static int toUInt16(byte[] array, int start) {
76 return (Utils.toUInt8(array[start]) << 8 | Utils.toUInt8(array[start + 1]));
79 public static int toUInt32(byte[] array, int start) {
80 return (Utils.toUInt8(array[start++]) << 24 | Utils.toUInt8(array[start++]) << 16
81 | Utils.toUInt8(array[start++]) << 8 | Utils.toUInt8(array[start]));
84 public static long toUInt64(byte[] array, int start) {
85 return ((long) Utils.toUInt8(array[start++]) << 24 | (long) toUInt8(array[start++]) << 16
86 | (long) toUInt8(array[start++]) << 8 | Utils.toUInt8(array[start]));