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

StringtoHex(byte[] value)
Return value as hex.
char[] chars = new char[value.length * 2];
for (int i = 0; i < value.length; i++) {
    int a = (value[i] >> 4) & 0x0F;
    int b = value[i] & 0x0F;
    chars[i * 2] = HEX_CHARS[a];
    chars[i * 2 + 1] = HEX_CHARS[b];
return new String(chars);
...
StringtoHex(char c)
to Hex
String table = "0123456789ABCDEF";
return table.charAt((c >> 8) & 0xF) + "" + table.charAt(c & 0xF);
StringtoHex(final byte b)
Converts a byte to hexadecimal
return String.format("%02X", b);
StringtoHex(final byte b)
to Hex
int i = (b >> 4) & 0x0F;
String tmp;
if (i < 10) {
    tmp = Integer.toString(i);
} else {
    tmp = new Character((char) (('A' + i) - 10)).toString();
i = b & 0x0F;
...
StringtoHex(final byte... bin)
Encodes a series of bytes into a hexidecimal string (potentially with leading zeroes) with no separators between each source byte
if (bin == null || bin.length == 0)
    return "";
final char[] buffer = new char[bin.length * 2];
for (int i = 0, j = 0; i < bin.length; i++) {
    final byte b = bin[i];
    buffer[j++] = hex[(b >> 4) & 0x0F];
    buffer[j++] = hex[b & 0x0F];
return new String(buffer);
StringtoHex(final byte[] ba)
Converts a byte array to a string of hexadecimal characters.
char[] hexdigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
StringBuffer sb = new StringBuffer("");
int ba_len = ba.length;
for (int i = 0; i < ba_len; i++) {
    sb.append(hexdigit[(ba[i] >> MAGIC_INTEGER_4) & MAGIC_INTEGER_OxOF]);
    sb.append(hexdigit[ba[i] & MAGIC_INTEGER_OxOF]);
return sb.toString();
...
StringtoHex(final byte[] ba)
to Hex
return toHex(ba, false);
StringtoHex(final byte[] bytes)
Converts the byte array into a HEX string.
final StringBuilder builder = new StringBuilder(bytes.length * 2);
for (final byte b : bytes) {
    builder.append(HEX_CHARS[(b >> 4) & 0xF]);
    builder.append(HEX_CHARS[b & 0xF]);
return builder.toString();
StringtoHex(final byte[] bytes)
Converts bytes to a string of hex.
char[] buf = new char[bytes.length * 2];
int pos = 0;
for (final byte aByte : bytes) {
    int index = (aByte & hMask) >> 4;
    char currChar = hexChars[index];
    buf[pos++] = currChar;
    index = (aByte & lMask);
    currChar = hexChars[index];
...
StringtoHex(final byte[] bytes)
Copied from the old Hex.java, needed for MD5 handling
StringBuilder sb = new StringBuilder();
for (final byte b : bytes) {
    String s = Integer.toHexString(0xff & b);
    if (s.length() < 2) {
        sb.append("0");
    sb.append(s);
return sb.toString();