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

@@ -64,7 +64,7 @@ public class PeCommand extends Command {
}
byte[] packetContent = HexTool.getByteArrayFromHexString(packet);
byte[] packetContent = HexTool.toBytes(packet);
InPacket inPacket = new ByteBufInPacket(Unpooled.wrappedBuffer(packetContent));
short packetId = inPacket.readShort();
final PacketHandler packetHandler = PacketProcessor.getProcessor(0, c.getChannel()).getHandler(packetId);

View File

@@ -24,26 +24,21 @@ package tools;
import constants.string.CharsetConstants;
import io.netty.buffer.ByteBufUtil;
import java.io.ByteArrayOutputStream;
import java.util.HexFormat;
// TODO: use HexFormat from Java 17
public class HexTool {
private static final char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
private static String toString(byte byteValue) {
int tmp = byteValue << 8;
char[] retstr = new char[]{HEX[(tmp >> 12) & 0x0F], HEX[(tmp >> 8) & 0x0F]};
return String.valueOf(retstr);
}
/**
* Convert a byte array to its hex string representation.
* Each byte value is equivalent 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) {
StringBuilder hexed = new StringBuilder();
for (byte aByte : bytes) {
hexed.append(toString(aByte));
hexed.append(' ');
}
return hexed.substring(0, hexed.length() - 1);
return HexFormat.ofDelimiter(" ").withUpperCase().formatHex(bytes);
}
/**
@@ -61,43 +56,6 @@ public class HexTool {
return input.replaceAll("\\s", "");
}
public static byte[] getByteArrayFromHexString(String hex) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int nexti = 0;
int nextb = 0;
boolean highoc = true;
outer:
for (; ; ) {
int number = -1;
while (number == -1) {
if (nexti == hex.length()) {
break outer;
}
char chr = hex.charAt(nexti);
if (chr >= '0' && chr <= '9') {
number = chr - '0';
} else if (chr >= 'a' && chr <= 'f') {
number = chr - 'a' + 10;
} else if (chr >= 'A' && chr <= 'F') {
number = chr - 'A' + 10;
} else {
number = -1;
}
nexti++;
}
if (highoc) {
nextb = number << 4;
highoc = false;
} else {
nextb |= number;
highoc = true;
baos.write(nextb);
}
}
// return toBytes(hex);
return baos.toByteArray();
}
public static String toStringFromAscii(final byte[] bytes) {
byte[] ret = new byte[bytes.length];
for (int x = 0; x < bytes.length; x++) {

View File

@@ -3307,7 +3307,7 @@ public class PacketCreator {
p.writeByte(msgType);
p.writeByte(speaker);
p.writeString(talk);
p.writeBytes(HexTool.getByteArrayFromHexString(endBytes));
p.writeBytes(HexTool.toBytes(endBytes));
return p;
}
@@ -6887,7 +6887,7 @@ public class PacketCreator {
public static Packet customPacket(String packet) {
OutPacket p = new ByteBufOutPacket();
p.writeBytes(HexTool.getByteArrayFromHexString(packet));
p.writeBytes(HexTool.toBytes(packet));
return p;
}

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