Use HexFormat for converting from bytes to hex string and the other way round

This commit is contained in:
P0nk
2022-02-14 18:37:30 +01:00
parent 0e32f439fb
commit 1daddbf302
4 changed files with 14 additions and 68 deletions

View File

@@ -1,10 +1,7 @@
package tools;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -21,23 +18,14 @@ class HexToolTest {
void hexStringWithSpacesToBytes() {
String hexString = "01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 7F FF";
byte[] expectedBytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 127, -1};
assertArrayEquals(expectedBytes, HexTool.getByteArrayFromHexString(hexString));
assertArrayEquals(expectedBytes, HexTool.toBytes(hexString));
}
@Test
void compactHexStringToBytes() {
String hexString = "0102030405060708090A0B0C0D0E0F10117FFF";
byte[] expectedBytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 127, -1};
assertArrayEquals(expectedBytes, HexTool.getByteArrayFromHexString(hexString));
}
@Test
@Disabled("The original method does not work this way, this test should be removed!")
void compactShortHexStringToBytes() {
String hexString = "0 2 3 4 5 A F";
byte[] expectedBytes = new byte[]{1, 2, 3, 4, 5, 10, 15};
System.out.println(Arrays.toString(HexTool.getByteArrayFromHexString(hexString)));
assertArrayEquals(expectedBytes, HexTool.getByteArrayFromHexString(hexString));
assertArrayEquals(expectedBytes, HexTool.toBytes(hexString));
}
@Test