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:
@@ -22,6 +22,7 @@
|
|||||||
package tools;
|
package tools;
|
||||||
|
|
||||||
import constants.string.CharsetConstants;
|
import constants.string.CharsetConstants;
|
||||||
|
import io.netty.buffer.ByteBufUtil;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
|
||||||
@@ -105,4 +106,14 @@ public class HexTool {
|
|||||||
return "";
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
22
src/test/java/tools/HexToolTest.java
Normal file
22
src/test/java/tools/HexToolTest.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user