Skip to content

Commit

Permalink
custom hex_encode/decode impl
Browse files Browse the repository at this point in the history
mostly for Java 11 compat, but also this is more efficient than the original, albeit lacking some error checks
  • Loading branch information
mcmonkey4eva committed Feb 7, 2021
1 parent 99a668c commit 67f2940
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Expand Up @@ -109,8 +109,9 @@
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand Down
Expand Up @@ -11,7 +11,6 @@
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.tags.core.EscapeTagBase;

import javax.xml.bind.DatatypeConverter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
Expand Down Expand Up @@ -2220,7 +2219,7 @@ else if (CoreUtilities.toLowerCase(element).contains(CoreUtilities.toLowerCase(c
// Encodes the element using hexadecimal encoding.
// -->
registerTag("hex_encode", (attribute, object) -> {
String encoded = DatatypeConverter.printHexBinary(object.element.getBytes());
String encoded = CoreUtilities.hexEncode(object.element.getBytes());
return new ElementTag(encoded);
});

Expand All @@ -2232,7 +2231,7 @@ else if (CoreUtilities.toLowerCase(element).contains(CoreUtilities.toLowerCase(c
// Decodes the element using hexadecimal encoding. Must be valid hexadecimal input.
// -->
registerTag("hex_decode", (attribute, object) -> {
String decoded = new String(DatatypeConverter.parseHexBinary(object.element));
String decoded = new String(CoreUtilities.hexDecode(object.element));
return new ElementTag(decoded);
});

Expand Down
Expand Up @@ -715,4 +715,41 @@ else if (m == 0) {
// actually has the most recent cost counts
return p[n];
}

private static byte[] valueOfHex = new byte[256];

public static char[] charForByte = "0123456789abcdef".toCharArray();

static {
for (byte i = 0; i < 10; i++) {
valueOfHex['0' + i] = i;
}
for (byte i = 0; i < 6; i++) {
valueOfHex['a' + i] = (byte) (i + 10);
valueOfHex['A' + i] = (byte) (i + 10);
}
}

public static byte[] hexDecode(String str) {
byte[] output = new byte[str.length() >> 1];
for (int i = 0; i < output.length; i++) {
char a = str.charAt(i << 1);
char b = str.charAt((i << 1) + 1);
byte valA = (byte) (valueOfHex[a] << 4);
byte valB = valueOfHex[b];
output[i] = (byte) (valA + valB);
}
return output;
}

public static String hexEncode(byte[] value) {
char[] output = new char[value.length * 2];
for (int i = 0; i < value.length; i++) {
byte valA = (byte) ((value[i] & 0xF0) >> 4);
byte valB = (byte) (value[i] & 0x0F);
output[i << 1] = charForByte[valA];
output[(i << 1) + 1] = charForByte[valB];
}
return new String(output);
}
}

0 comments on commit 67f2940

Please sign in to comment.