org.opensky.libadsb.tools Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of libadsb Show documentation
Show all versions of libadsb Show documentation
A Mode S and ADS-B decoding library for Java
package org.opensky.libadsb;
/*
* This file is part of org.opensky.libadsb.
*
* org.opensky.libadsb is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* org.opensky.libadsb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with org.opensky.libadsb. If not, see .
*/
/**
* Some useful functions when working with libadsb. Mostly we need these
* functions since the library often works with arrays of bytes which are
* not really readable for humans or basic operations are missing.
* @author Matthias Schäfer ([email protected])
*/
public class tools {
private static final char[] hexDigits =
{'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* Converts a byte into a hex string (e.g. 164 -> "a4")
* @param b input byte
* @return hex representation of input byte
*/
public static String toHexString(byte b) {
final char[] out = new char[2];
out[0] = hexDigits[(0xF0 & b) >>> 4];
out[1] = hexDigits[0x0F & b];
return new String(out);
}
/**
* Converts an array of bytes in a hex string; Taken from
* org.apache.commons.codec.binary.Hex.
* @param bytes array of bytes
* @return concatenated hex representation of input byte array
*/
public static String toHexString(byte[] bytes) {
final int l = bytes.length;
final char[] out = new char[l << 1];
for (int i = 0, j = 0; i < l; i++) {
out[j++] = hexDigits[(0xF0 & bytes[i]) >>> 4];
out[j++] = hexDigits[0x0F & bytes[i]];
}
return new String(out);
}
/**
* Converts a hex string to an array of bytes.
* Source: https://stackoverflow.com/a/140861/3485023
* @param str the hex string to convert
* @return the byte array
*/
public static byte[] hexStringToByteArray(String str) {
int len = str.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(str.charAt(i), 16) << 4)
+ Character.digit(str.charAt(i+1), 16));
}
return data;
}
/**
* Compares two byte arrays element by element
* @param array1 first array
* @param array2 second array
* @return array1 == array2
*/
public static boolean areEqual(byte[] array1, byte[] array2) {
if (array1.length != array2.length) return false;
for (int i=0; i