58 lines
1.9 KiB
Java
58 lines
1.9 KiB
Java
/*
|
|
* Copyright 2021 Paul Schaub, @maybeWeCouldStealAVan, @Dave L.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
package sop.util;
|
|
|
|
public class HexUtil {
|
|
|
|
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
|
|
|
|
/**
|
|
* Encode a byte array to a hex string.
|
|
*
|
|
* @see <a href="https://stackoverflow.com/a/9855338">
|
|
* How to convert a byte array to a hex string in Java?</a>
|
|
* @param bytes
|
|
* @return
|
|
*/
|
|
public static String bytesToHex(byte[] bytes) {
|
|
char[] hexChars = new char[bytes.length * 2];
|
|
for (int j = 0; j < bytes.length; j++) {
|
|
int v = bytes[j] & 0xFF;
|
|
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
|
|
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
|
|
}
|
|
return new String(hexChars);
|
|
}
|
|
|
|
/**
|
|
* Decode a hex string into a byte array.
|
|
*
|
|
* @see <a href="https://stackoverflow.com/a/140861">
|
|
* Convert a string representation of a hex dump to a byte array using Java?</a>
|
|
* @param s hex string
|
|
* @return decoded byte array
|
|
*/
|
|
public static byte[] hexToBytes(String s) {
|
|
int len = s.length();
|
|
byte[] data = new byte[len / 2];
|
|
for (int i = 0; i < len; i += 2) {
|
|
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
|
|
+ Character.digit(s.charAt(i + 1), 16));
|
|
}
|
|
return data;
|
|
}
|
|
}
|