Add simple hex utils that delegate to Netty

The goal is to use libraries for this sort of common stuff
This commit is contained in:
P0nk
2021-07-15 23:48:47 +02:00
parent cce85ea663
commit 12a415e3d5
2 changed files with 33 additions and 0 deletions

View File

@@ -22,6 +22,7 @@
package tools;
import constants.string.CharsetConstants;
import io.netty.buffer.ByteBufUtil;
import java.io.ByteArrayOutputStream;
@@ -105,4 +106,14 @@ public class HexTool {
return "";
}
/**
* 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);
}
}

View File

@@ -0,0 +1,22 @@
package tools;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class HexToolTest {
@Test
void upperCaseHexToBytesAndBack() {
String hex = "A1B2C3";
byte[] bytes = HexTool.hexToBytes(hex);
assertEquals(hex, HexTool.bytesToHex(bytes));
}
@Test
void mixedCaseHexToBytesAndBack() {
String hex = "aB5DaA";
byte[] bytes = HexTool.hexToBytes(hex);
assertEquals(hex.toUpperCase(), HexTool.bytesToHex(bytes));
}
}