Java Utililty Methods Hex Convert To

List of utility methods to do Hex Convert To

Description

The list of methods to do Hex Convert To are organized into topic(s).

Method

bytefromHex(final String hex)
from Hex
if (hex == null)
    throw new IllegalArgumentException("Hex strings may not be null");
if (hex.length() % 2 != 0)
    throw new IllegalArgumentException("Hex strings must have length 2");
return (byte) (dehex(hex.charAt(0)) << 4 | dehex(hex.charAt(1)));
intfromHex(final String hexValue)
from Hex
return (int) Long.parseLong(hexValue, 16);
byte[]fromHex(final String s)
from Hex
return fromHex(s.replace(" ", "").toCharArray());
byte[]fromHex(final String string, final int offset, final int count)
Converts the specified array of hex characters into an array of bytes (low byte first).
if (offset >= string.length())
    throw new IllegalArgumentException("Offset is greater than the length (" + offset + " >= "
            + string.length() + ").");
if ((count & 0x01) != 0)
    throw new IllegalArgumentException("Count is not divisible by two (" + count + ").");
final int charCount = Math.min((string.length() - offset), count);
final int upperBound = offset + charCount;
final byte[] bytes = new byte[charCount >>> 1];
...
byte[]fromHex(String bytesString)
from Hex
int len = bytesString.length() / 2;
if (bytesString.length() % 2 != 0) {
    throw new IllegalArgumentException("Bytes Hex string length has to be even number");
byte[] out = new byte[len];
for (int i = 0; i < len; i++) {
    int pos = i * 2;
    byte b = (byte) Integer.valueOf(bytesString.substring(pos, pos + 2), 16).intValue();
...
byte[]fromHex(String encoded)
Decode hex string to a byte array
if (encoded == null)
    throw new NullPointerException();
int lengthData = encoded.length();
if (lengthData % 2 != 0)
    throw new IllegalStateException("bad string :" + encoded);
char[] binaryData = encoded.toCharArray();
int lengthDecode = lengthData / 2;
byte[] decodedData = new byte[lengthDecode];
...
byte[]fromHex(String hex)
from Hex
if (hex.length() % 2 != 0) {
    hex = "0" + hex;
int byteCount = hex.length() / 2;
byte[] bytes = new byte[byteCount];
for (int i = 0; i < byteCount; i++) {
    int index = i * 2;
    char upper = hex.charAt(index);
...
byte[]fromHex(String hex)
Converts a string of hexadecimal characters into a byte array.
byte[] binary = new byte[hex.length() / 2];
for (int i = 0; i < binary.length; i++) {
    binary[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
return binary;
byte[]fromHex(String hex)
Decodes a string of hex octets to a byte array.
if (hex == null) {
    return null;
hex = hex.replace(" ", "");
if (hex.length() % 2 == 1) {
    return null;
byte[] bytes = new byte[hex.length() / 2];
...
intfromHex(String hex)
Returns the integer value of an hexadecimal number (base 16).
return Integer.parseInt(hex, 16);