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(byte[] buf)
Convenience call for #toHexString(byte[],String,int) , where sep = null; lineLen = Integer.MAX_VALUE.
return toHexString(buf, null, Integer.MAX_VALUE);
StringtoHexString(byte[] buf)
http://homepage2.nifty.com/igat/igapyon/diary/2002/ig021213.html
String s = "";
for (int i = 0; i < buf.length; i++) {
    int n = buf[i] & 0xff;
    if (n < 16) {
        s += " 0";
    } else {
        s += " ";
    s += Integer.toHexString(n).toUpperCase();
return s;
StringtoHexString(byte[] buf, int offset, int len)
to Hex String
if (buf == null)
    return "null";
StringBuilder b = new StringBuilder();
for (int i = offset, j = 0; j < len && i < buf.length; i++, j++) {
    int idx0 = (buf[i] & 0xff) >> 4;
    int idx1 = buf[i] & 0xf;
    b.append(hexChars[idx0]);
    b.append(hexChars[idx1]);
...
StringtoHexString(byte[] buffer)
Converts a buffer of bytes into a string of hex values.
final StringBuilder result = new StringBuilder();
for (int i = 0; i < buffer.length; i++) {
    String hex = Integer.toHexString(buffer[i]);
    if (hex.length() == 1) {
        hex = "0" + hex;
    result.append(hex.substring(hex.length() - 2));
return result.toString();
StringtoHexString(byte[] byteArray)
Converts a byte array to hex string without leading 0x.
if (byteArray == null) {
    return null;
final StringBuilder sb = new StringBuilder(2 + 2 * byteArray.length);
for (final byte b : byteArray) {
    sb.append(hexChars.charAt((b & 0xF0) >> 4)).append(hexChars.charAt((b & 0x0F)));
return sb.toString();
...
StringtoHexString(byte[] byteArray)
Takes a byte array and returns it HEX representation.
if (byteArray != null && byteArray.length != 0) {
    StringBuilder builder = new StringBuilder(byteArray.length * 3);
    for (int i = 0; i < byteArray.length; i++) {
        builder.append(String.format("%02X", 0xFF & byteArray[i]));
        if (i < byteArray.length - 1) {
            builder.append(' ');
    return builder.toString();
} else {
    return "--";
StringtoHexString(byte[] byteArray)
to Hex String
final StringBuilder ret = new StringBuilder();
for (final byte b : byteArray) {
    ret.append(toHexString(b));
return ret.toString();
StringtoHexString(byte[] byteArray)
to Hex String
return toHexString(byteArray, "");
StringtoHexString(byte[] byteArray)
Converts a byte array into an hexadecimal string.
final char[] hexChars = new char[2 * byteArray.length];
int i = 0;
for (final byte b : byteArray) {
    hexChars[i++] = HEXDIGITS[(b >> 4) & 0xF];
    hexChars[i++] = HEXDIGITS[b & 0xF];
return new String(hexChars);
StringtoHexString(byte[] byteArray, boolean withSpaces)
to Hex String
StringBuilder builder = new StringBuilder();
for (int i = 0; i < byteArray.length; i++) {
    if (withSpaces && i > 0) {
        builder.append(" ");
    builder.append(HEXCHARS[(byteArray[i] >> 4) & 0xF]);
    builder.append(HEXCHARS[byteArray[i] & 0xF]);
return builder.toString();