|
@@ -35,24 +35,27 @@ public class ByteUtil {
|
|
|
public static String toHexAscii(byte[] bytes) {
|
|
|
int len = bytes.length;
|
|
|
StringWriter sw = new StringWriter(len * 2);
|
|
|
- for (int i = 0; i < len; ++i)
|
|
|
- addHexAscii(bytes[i], sw);
|
|
|
+ for (int i = 0; i < len; ++i) {
|
|
|
+ addHexAscii(bytes[i], sw);
|
|
|
+ }
|
|
|
return sw.toString();
|
|
|
}
|
|
|
|
|
|
public static String toLowercaseHexAscii(byte[] bytes) {
|
|
|
int len = bytes.length;
|
|
|
StringWriter sw = new StringWriter(len * 2);
|
|
|
- for (int i = 0; i < len; ++i)
|
|
|
- addLowercaseHexAscii(bytes[i], sw);
|
|
|
+ for (int i = 0; i < len; ++i) {
|
|
|
+ addLowercaseHexAscii(bytes[i], sw);
|
|
|
+ }
|
|
|
return sw.toString();
|
|
|
}
|
|
|
|
|
|
public static byte[] fromHexAscii(String s) throws NumberFormatException {
|
|
|
try {
|
|
|
int len = s.length();
|
|
|
- if ((len % 2) != 0)
|
|
|
- throw new NumberFormatException("Hex ascii must be exactly two digits per byte.");
|
|
|
+ if ((len % 2) != 0) {
|
|
|
+ throw new NumberFormatException("Hex ascii must be exactly two digits per byte.");
|
|
|
+ }
|
|
|
|
|
|
int out_len = len / 2;
|
|
|
byte[] out = new byte[out_len];
|
|
@@ -85,31 +88,34 @@ public class ByteUtil {
|
|
|
}
|
|
|
|
|
|
private static int fromHexDigit(int c) throws NumberFormatException {
|
|
|
- if (c >= 0x30 && c < 0x3A)
|
|
|
- return c - 0x30;
|
|
|
- else if (c >= 0x41 && c < 0x47)
|
|
|
- return c - 0x37;
|
|
|
- else if (c >= 0x61 && c < 0x67)
|
|
|
- return c - 0x57;
|
|
|
- else
|
|
|
- throw new NumberFormatException('\'' + c + "' is not a valid hexadecimal digit.");
|
|
|
+ if (c >= 0x30 && c < 0x3A) {
|
|
|
+ return c - 0x30;
|
|
|
+ } else if (c >= 0x41 && c < 0x47) {
|
|
|
+ return c - 0x37;
|
|
|
+ } else if (c >= 0x61 && c < 0x67) {
|
|
|
+ return c - 0x57;
|
|
|
+ } else {
|
|
|
+ throw new NumberFormatException('\'' + c + "' is not a valid hexadecimal digit.");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private static char toHexDigit(int h) {
|
|
|
char out;
|
|
|
- if (h <= 9)
|
|
|
- out = (char) (h + 0x30);
|
|
|
- else
|
|
|
- out = (char) (h + 0x37);
|
|
|
+ if (h <= 9) {
|
|
|
+ out = (char) (h + 0x30);
|
|
|
+ } else {
|
|
|
+ out = (char) (h + 0x37);
|
|
|
+ }
|
|
|
return out;
|
|
|
}
|
|
|
|
|
|
private static char toLowercaseHexDigit(int h) {
|
|
|
char out;
|
|
|
- if (h <= 9)
|
|
|
- out = (char) (h + 0x30);
|
|
|
- else
|
|
|
- out = (char) (h + 0x57);
|
|
|
+ if (h <= 9) {
|
|
|
+ out = (char) (h + 0x30);
|
|
|
+ } else {
|
|
|
+ out = (char) (h + 0x57);
|
|
|
+ }
|
|
|
return out;
|
|
|
}
|
|
|
|