]> git.basschouten.com Git - openhab-addons.git/blob
f8c1692243ba06a20b9ed70eb3a7b18de9f81a55
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.miele.internal;
14
15 import java.nio.charset.StandardCharsets;
16
17 /**
18  * The {@link ExtendedDeviceStateUtil} class contains utility methods for parsing
19  * ExtendedDeviceState information
20  *
21  * @author Jacob Laursen - Added power/water consumption channels
22  */
23 public class ExtendedDeviceStateUtil {
24     private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII);
25
26     /**
27      * Convert byte array to hex representation.
28      */
29     public static String bytesToHex(byte[] bytes) {
30         byte[] hexChars = new byte[bytes.length * 2];
31         for (int j = 0; j < bytes.length; j++) {
32             int v = bytes[j] & 0xFF;
33             hexChars[j * 2] = HEX_ARRAY[v >>> 4];
34             hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
35         }
36
37         return new String(hexChars, StandardCharsets.UTF_8);
38     }
39
40     /**
41      * Convert string consisting of 8 bit characters to byte array.
42      * Note: This simple operation has been extracted and put here to document
43      * and ensure correct behavior for 8 bit characters that should be turned
44      * into single bytes without any UTF-8 encoding.
45      */
46     public static byte[] stringToBytes(String input) {
47         return input.getBytes(StandardCharsets.ISO_8859_1);
48     }
49 }