Java Utililty Methods Hex Calculate

List of utility methods to do Hex Calculate

Description

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

Method

StringtoHexString(final byte b)
Converts a byte into its hexadecimal representation, padding with a leading zero to get an even number of characters.
String hexString = Integer.toHexString(b & 0xFF);
if (hexString.length() % 2 != 0) {
    hexString = "0" + hexString;
return hexString;
StringtoHexString(final byte b)
to Hex String
return toHexString(toByteArray(b));
StringtoHexString(final byte hex)
to Hex String
return "0x" + Integer.toHexString(hex & 0x000000FF);
StringtoHexString(final byte value)
Returns a unsigned hex String of the byte value.
String result = Integer.toHexString(toUnsigned(value)).toUpperCase();
if (result.length() == 1) {
    result = "0x0" + result;
} else {
    result = "0x" + result;
return result;
StringtoHexString(final byte[] arr)
to Hex String
final StringBuilder buffer = new StringBuilder();
buffer.append('[');
if (arr.length > 0)
    buffer.append(Integer.toHexString(arr[0]));
for (int i = 1; i < arr.length; i++) {
    buffer.append(' ');
    buffer.append(Integer.toHexString(arr[i]));
buffer.append(']');
return buffer.toString();
StringtoHexString(final byte[] array)
to Hex String
return toStringHex(array, 0, array.length);
StringtoHexString(final byte[] b)
Converts a byte array to a hex string representing it.
final StringBuilder sb = new StringBuilder();
for (final byte element : b) {
    sb.append(hexChar[(element & 0xf0) >>> 4]);
    sb.append(hexChar[element & 0x0f]);
return sb.toString();
StringtoHexString(final byte[] bs)
to Hex String
final int len;
if (bs != null && (len = bs.length) != 0) {
    final char[] cs = new char[len << 1];
    final char[] myDigits = DIGITS;
    byte b;
    for (int i = 0, j = 0; i < len; i++) {
        cs[j++] = myDigits[((b = bs[i]) >>> 4) & 0xF];
        cs[j++] = myDigits[b & 0xF];
...
StringtoHexString(final byte[] bs, final char[] myDigits)
to Hex String
final int len;
if (bs == null || (len = bs.length) == 0) {
    return null;
final char[] cs = new char[len << 1];
byte b;
for (int i = 0, j = 0; i < len; i++) {
    cs[j++] = myDigits[((b = bs[i]) >>> 4) & 0xF];
...
StringtoHexString(final byte[] buffer)
Converts an array of bytes into an hex string.
final char[] charBuffer = new char[buffer.length * 2];
for (int i = 0; i < buffer.length; ++i) {
    charBuffer[i * 2] = HEX_DIGITS[(buffer[i] >> BITS_SIZE_OF_NIBBLE) & INT_BYTE_LOW_NIBBLE_MASK];
    charBuffer[i * 2 + 1] = HEX_DIGITS[buffer[i] & INT_BYTE_LOW_NIBBLE_MASK];
return new String(charBuffer);