mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-12 08:37:25 -05:00
* #1455 Add support for airspy hf+/discovery tuners * #1455 testing updates --------- Co-authored-by: Dennis Sheirer <dsheirer@github.com>
This commit is contained in:
65
src/main/java/io/github/dsheirer/util/ByteUtil.java
Normal file
65
src/main/java/io/github/dsheirer/util/ByteUtil.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* *****************************************************************************
|
||||
* Copyright (C) 2014-2023 Dennis Sheirer
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>
|
||||
* ****************************************************************************
|
||||
*/
|
||||
|
||||
package io.github.dsheirer.util;
|
||||
|
||||
/**
|
||||
* Utilities for working with bytes and byte arrays
|
||||
*/
|
||||
public class ByteUtil
|
||||
{
|
||||
/**
|
||||
* Converts a byte array to an integer using big endian format.
|
||||
* @param bytes containing four bytes.
|
||||
* @param offset into the byte array to start parsing.
|
||||
* @return signed integer value.
|
||||
*/
|
||||
public static int toInteger(byte[] bytes, int offset)
|
||||
{
|
||||
if(bytes == null || bytes.length < (offset + 4))
|
||||
{
|
||||
throw new IllegalArgumentException("Conversion to integer requires byte array with at least 4 bytes - " +
|
||||
"length:" + bytes.length + " offset:" + offset);
|
||||
}
|
||||
|
||||
int value = (bytes[offset + 3] & 0xFF) << 24;
|
||||
value += (bytes[offset + 2] & 0xFF) << 16;
|
||||
value += (bytes[offset + 1] & 0xFF) << 8;
|
||||
value += (bytes[offset] & 0xFF);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the byte array as a string of hexadecimal values.
|
||||
* @param bytes to format
|
||||
* @return hex string
|
||||
*/
|
||||
public static String toHexString(byte[] bytes)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for(byte b : bytes)
|
||||
{
|
||||
sb.append(String.format("%02X", b));
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user