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

byte[]fromHex(String hexString)
from Hex
String[] data = hexString.trim().split(" ");
byte[] result = new byte[data.length];
for (int i = 0; i < data.length; i++) {
    result[i] = (byte) Integer.parseInt(data[i]);
return result;
byte[]fromHex(String hexString)
from Hex
if (hexString == null) {
    return null;
int len = hexString.length();
if (len % 2 != 0) {
    throw new IllegalArgumentException();
byte[] data = new byte[len / 2];
...
intfromHex(String input, int max)
Converts a string from hex to integer.
try {
    int value = Integer.parseInt(input.toLowerCase(), 16);
    if (value < 0 || value > max) {
        throw new IllegalArgumentException("Out of range: " + input);
    return value;
} catch (NumberFormatException e) {
    throw new IllegalArgumentException("Not valid hex: " + input);
...
intfromHex(String s)
from Hex
return Integer.parseInt(s, 16);
byte[]fromHex(String s)
from Hex
if (s.length() % 2 != 0) {
    throw new IllegalArgumentException("Hex " + s + " must be divisible by two");
byte[] bytes = new byte[s.length() / 2];
for (int i = 0; i < bytes.length; i++) {
    char left = s.charAt(i * 2);
    char right = s.charAt(i * 2 + 1);
    byte b = (byte) ((getValue(left) << 4) | (getValue(right) & 0xF));
...
byte[]fromHex(String s)
from Hex
if (s != null) {
    try {
        StringBuilder sb = new StringBuilder(s.length());
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (!Character.isWhitespace(ch)) {
                sb.append(ch);
        s = sb.toString();
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            int hi = (Character.digit(s.charAt(i), 16) << 4);
            int low = Character.digit(s.charAt(i + 1), 16);
            if (hi >= 256 || low < 0 || low >= 16) {
                return null;
            data[i / 2] = (byte) (hi | low);
        return data;
    } catch (Exception ignored) {
return null;
byte[]fromHex(String s)
Converts a hex string representation of bytes into a byte array
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;
byte[]fromHex(String s)
from Hex
byte bs[] = new byte[s.length() / 2];
for (int i = 0; i < bs.length; i++)
    bs[i] = (byte) Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16);
return bs;
bytefromHex(String s)
from Hex
if (s.length() != 2) {
    throw new IllegalArgumentException("Invalid length of string.");
char c1 = s.charAt(0);
char c2 = s.charAt(1);
int n1 = HEX_STRING.indexOf(c1);
int n2 = HEX_STRING.indexOf(c2);
if (n1 == (-1)) {
...
byte[]fromHex(String s)
Converts hex string to byte array.
if (!isHexString(s)) {
    throw new IllegalArgumentException("Invalid hex 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;