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[] raw)
to Hex
final String HEXES = "0123456789ABCDEF";
if (raw == null) {
    return null;
final StringBuilder hex = new StringBuilder(2 * raw.length);
for (final byte b : raw) {
    hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F))).append(" ");
return hex.toString();
StringtoHex(byte[] raw)
to Hex
if (raw == null) {
    return null;
int index = 0;
final StringBuilder hex = new StringBuilder(2 * raw.length);
for (final byte b : raw) {
    hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
    if (index < raw.length - 1) {
...
StringtoHex(byte[] raw)
to Hex
if (raw.length != 16)
    throw new IllegalArgumentException("length must be 16, not " + raw.length);
final char[] hex = new char[32];
short i = 0;
for (final byte b : raw) {
    hex[i] = HEXES[(b & 0xF0) >> 4];
    hex[i + 1] = HEXES[(b & 0x0F)];
    i += 2;
...
StringToHex(byte[] src)
To Hex
String res = "";
for (int i = 0; i < src.length; i++) {
    res += String.format("%02X", src[i] & 0xFF);
return res.toLowerCase();
byte[]toHex(byte[] src)
to Hex
int l = src.length;
byte dst[] = new byte[l * 2];
for (int i = 0, j = 0; i < l; i++) {
    int k = src[i];
    k &= (int) 0xFF;
    dst[j++] = CHEX[k >> 4];
    dst[j++] = CHEX[k & 0x0F];
return dst;
StringtoHex(byte[] text)
Utility method which converts a byte[] to a hexadecimal string of characters, in lower case
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < text.length; i++) {
    buffer.append(HEX_ARRAY[0x0f & (text[i] >> 4)]);
    buffer.append(HEX_ARRAY[0x0f & text[i]]);
return buffer.toString();
StringtoHex(byte[] v)
to Hex
String out = "";
for (int i = 0; i < v.length; i++)
    out = out + hex[(v[i] >> 4) & 0xF] + hex[v[i] & 0xF];
return (out);
StringtoHex(byte[] val)
to Hex
StringBuffer out = new StringBuffer();
for (int i = 0, j = val.length; i < j; i++) {
    String ve = Integer.toString((((int) ((char) val[i])) & 0xFF), 16);
    if (ve.length() == 1) {
        ve = "0" + ve;
    out.append(ve);
return out.toString();
StringtoHex(byte[] val, int start, int len)
Can be used to encode values that contain invalid XML characters.
StringBuffer buf = new StringBuffer();
for (int i = 0; i < len; i++) {
    byte b = val[start + i];
    buf.append(DEC2HEX[(b & 0xf0) >> 4]);
    buf.append(DEC2HEX[b & 0x0f]);
return buf.toString();
StringtoHex(byte[] value)
to Hex
final StringBuilder b = new StringBuilder();
for (byte c : value)
    b.append(hex[(c >>> 4) & 0xf]).append(hex[c & 0xf]);
return "0x" + b.toString();