View Javadoc
1   /*
2    * Copyright © 2016 Greg Chabala
3    *
4    * This file is part of brick-control-lab.
5    *
6    * brick-control-lab is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   *
11   * brick-control-lab is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with brick-control-lab.  If not, see http://www.gnu.org/licenses/.
18   */
19  package org.chabala.brick.controllab.sensor;
20  
21  /**
22   * Helper methods for making readable binary strings of data bytes.
23   */
24  final class BinaryStringFormatter {
25  
26      /**
27       * Takes a byte array of two bytes and returns a string of those bytes
28       * represented in binary.
29       * @param highLow byte array containing two bytes
30       * @return a sixteen character string of zeros and ones
31       */
32      static String printInBinary(byte[] highLow) {
33          return printByteInBinary(highLow[0]) + printByteInBinary(highLow[1]);
34      }
35  
36      /**
37       * Takes a single byte of data and returns an eight character string
38       * representing that byte in binary. Leading zeros are preserved.
39       * @param data a single byte of data
40       * @return an eight character string of zeros and ones
41       */
42      static String printByteInBinary(byte data) {
43          return Integer.toBinaryString(Byte.toUnsignedInt(data) + 0x100).substring(1);
44      }
45  
46      private BinaryStringFormatter() {
47          throw new UnsupportedOperationException();
48      }
49  }