Consolidate HexTool methods and add more tests
This commit is contained in:
@@ -22,30 +22,44 @@
|
||||
package tools;
|
||||
|
||||
import constants.string.CharsetConstants;
|
||||
import io.netty.buffer.ByteBufUtil;
|
||||
|
||||
import java.util.HexFormat;
|
||||
|
||||
// TODO: use HexFormat from Java 17
|
||||
/**
|
||||
* Handles converting back and forth from byte arrays to hex strings.
|
||||
*/
|
||||
public class HexTool {
|
||||
|
||||
/**
|
||||
* Convert a byte array to its hex string representation.
|
||||
* Each byte value is equivalent to two hex characters delimited by a space.
|
||||
* Convert a byte array to its hex string representation (upper case).
|
||||
* Each byte value is converted to two hex characters delimited by a space.
|
||||
*
|
||||
* @param bytes Byte array to convert to a hex string.
|
||||
* Example: {1, 16, 127, -1} is converted to "01 F0 7F FF"
|
||||
* @return The hex string
|
||||
*/
|
||||
public static String toString(byte[] bytes) {
|
||||
public static String toHexString(byte[] bytes) {
|
||||
return HexFormat.ofDelimiter(" ").withUpperCase().formatHex(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a hex string to its byte array representation. Two consecutive hex characters are equivalent to one byte.
|
||||
* Convert a byte array to its hex string representation (upper case).
|
||||
* Like {@link #toHexString(byte[]) HexTool.toString}, but with no space delimiter.
|
||||
*
|
||||
* @param hexString Hex string to convert to bytes. Hex character pairs may be delimited by a space or not (compact)
|
||||
* @return The compact hex string
|
||||
*/
|
||||
public static String toCompactHexString(byte[] bytes) {
|
||||
return HexFormat.of().withUpperCase().formatHex(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a hex string to its byte array representation. Two consecutive hex characters are converted to one byte.
|
||||
*
|
||||
* @param hexString Hex string to convert to bytes. May be lower or upper case, and hex character pairs may be
|
||||
* delimited by a space or not.
|
||||
* Example: "01 10 7F FF" is converted to {1, 16, 127, -1}.
|
||||
* The following hex strings are considered identical and are converted to the same byte array:
|
||||
* "01 10 7F FF", "01107FFF", "01 10 7f ff", "01107fff"
|
||||
* @return The byte array
|
||||
*/
|
||||
public static byte[] toBytes(String hexString) {
|
||||
@@ -57,27 +71,19 @@ public class HexTool {
|
||||
}
|
||||
|
||||
public static String toStringFromAscii(final byte[] bytes) {
|
||||
byte[] ret = new byte[bytes.length];
|
||||
for (int x = 0; x < bytes.length; x++) {
|
||||
if (bytes[x] < 32 && bytes[x] >= 0) {
|
||||
ret[x] = '.';
|
||||
byte[] filteredBytes = new byte[bytes.length];
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
if (isSpecialCharacter(bytes[i])) {
|
||||
filteredBytes[i] = '.';
|
||||
} else {
|
||||
int chr = ((short) bytes[x]) & 0xFF;
|
||||
ret[x] = (byte) chr;
|
||||
filteredBytes[i] = (byte) (bytes[i] & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
return new String(ret, CharsetConstants.CHARSET);
|
||||
return new String(filteredBytes, CharsetConstants.CHARSET);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get upper case hex dump
|
||||
*/
|
||||
public static String bytesToHex(byte[] bytes) {
|
||||
return ByteBufUtil.hexDump(bytes).toUpperCase();
|
||||
}
|
||||
|
||||
public static byte[] hexToBytes(String hex) {
|
||||
return ByteBufUtil.decodeHexDump(hex);
|
||||
private static boolean isSpecialCharacter(byte asciiCode) {
|
||||
return asciiCode >= 0 && asciiCode <= 31;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user