]> git.basschouten.com Git - openhab-addons.git/blob
35492d46c0a9347a418d7ca0d8b545ff874290bd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.sinope.internal.util;
14
15 import java.util.Arrays;
16
17 /**
18  * The Class ByteUtil.
19  *
20  * @author Pascal Larin - Initial contribution
21  */
22 public class ByteUtil {
23
24     /**
25      * Reverse.
26      *
27      * @param array to reverse
28      * @return the reserved in byte[]
29      */
30     public static byte[] reverse(byte[] array) {
31         if (array == null) {
32             return null;
33         }
34         byte[] r = Arrays.copyOf(array, array.length);
35         int i = 0;
36         int j = r.length - 1;
37         byte tmp;
38         while (j > i) {
39             tmp = r[j];
40             r[j] = r[i];
41             r[i] = tmp;
42             j--;
43             i++;
44         }
45         return r;
46     }
47
48     /**
49      * To string.
50      *
51      * @param buf the buf
52      * @return the string
53      */
54     public static String toString(byte[] buf) {
55         StringBuilder sb = new StringBuilder();
56         for (byte b : buf) {
57             sb.append(String.format("0x%02X ", b));
58         }
59         return sb.toString();
60     }
61 }