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[]fromHexString(byte abyte0[], int i)
from Hex String
int j = 0;
if (abyte0[0] == 48 && (abyte0[1] == 120 || abyte0[1] == 88)) {
    j += 2;
    i -= 2;
int k = i / 2;
byte abyte1[] = new byte[k];
for (int l = 0; l < k;) {
...
byte[]fromHexString(final String hexaString)
Creates a ByteBuffer whose content is represented as hexadecimal in the given String.
if ((hexaString.length() % 2) != 0) {
    throw new IllegalArgumentException("Input string must contain an even number of characters");
int length = hexaString.length();
byte[] buf = new byte[length / 2];
for (int i = 0; i < length; i += 2) {
    buf[i / 2] = (byte) ((Character.digit(hexaString.charAt(i), 16) << 4)
            + Character.digit(hexaString.charAt(i + 1), 16));
...
byte[]fromHexString(final String hexString)
Converts the provided String of hexadecimal numbers (with optional leading "0x") into a byte[] of the same value.
if (hexString == null) {
    return null;
String str = hexString.trim().toLowerCase();
if (str.startsWith("0x")) {
    str = str.substring(2);
int numChars = str.length();
...
byte[]fromHexString(final String s)
Creates byte array representation of HEX string.
int length = s.length() / 2;
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++) {
    bytes[i] = (byte) (Character.digit(s.charAt(i * 2), 16) << 4
            | Character.digit(s.charAt(i * 2 + 1), 16));
return bytes;
byte[]fromHexString(final String str)
from Hex String
if ((str.length() % 2) != 0)
    throw new IllegalArgumentException(ERROR_INPUT_STRING_NUM_CHARS);
byte[] result = new byte[str.length() / 2];
for (int i = 0; i < str.length() / 2; i++) {
    result[i] = (Integer.decode(STR_HEX_PREFIX + str.substring(i * 2, (i + 1) * 2))).byteValue();
return result;
byte[]fromHexString(String encoded)
Returns a byte array of a given string by converting each character of the string to a number base 16
if ((encoded.length() % 2) != 0)
    throw new IllegalArgumentException("Input string must contain an even number of characters."); 
final byte result[] = new byte[encoded.length() / 2];
final char enc[] = encoded.toCharArray();
for (int i = 0; i < enc.length; i += 2) {
    StringBuilder curr = new StringBuilder(2);
    curr.append(enc[i]).append(enc[i + 1]);
    result[i / 2] = (byte) Integer.parseInt(curr.toString(), 16);
...
byte[]fromHexString(String encoded)
from Hex String
encoded = removeSpaces(encoded);
if (encoded.length() == 0) {
    return new byte[0];
if ((encoded.length() % 2) != 0) {
    throw new IllegalArgumentException(
            "Input string must contain an even number of characters: " + encoded);
final byte result[] = new byte[encoded.length() / 2];
final char enc[] = encoded.toCharArray();
for (int i = 0; i < enc.length; i += 2) {
    StringBuilder curr = new StringBuilder(2);
    curr.append(enc[i]).append(enc[i + 1]);
    result[i / 2] = (byte) Integer.parseInt(curr.toString(), 16);
return result;
byte[]fromHexString(String hex)
from Hex String
return fromHex(hex.getBytes());
byte[]fromHexString(String hex)
Converts the given hex string to a byte array.
int len = hex.length();
if (len % 2 != 0)
    throw new IllegalArgumentException("Not a hex string");
byte[] bytes = new byte[len / 2];
for (int i = 0, j = 0; i < len; i += 2, j++) {
    int high = hexDigitToInt(hex.charAt(i));
    int low = hexDigitToInt(hex.charAt(i + 1));
    bytes[j] = (byte) ((high << 4) + low);
...
byte[]fromHexString(String hexString)
From hex string.
if (hexString != null) {
    int strlen = hexString.length();
    int blen = strlen / 2 + strlen % 2;
    byte[] barray = new byte[blen];
    int i = 0;
    int j = 0;
    if (strlen % 2 > 0) {
        hexString = "0" + hexString;
...