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(String txt)
to Hex
return toHex(txt.getBytes());
StringtoHex(String val)
to Hex
String ret = "0x";
for (int i = 0; i < val.length(); i++) {
    ret += toHex(val.charAt(i), 2);
return ret;
voidtoHex(StringBuffer buf, long value, int width)
to Hex
if (value > (long) 1 << width * 4) {
    buf.append(Long.toHexString(value).toUpperCase());
    return;
for (int cntr = width - 1; cntr >= 0; cntr--)
    buf.append(HEX_CHARS[(int) (value >> (cntr * 4)) & 0xf]);
voidtoHex(StringBuilder s, byte b)
to Hex
char hi = Character.forDigit((b >> 4) & 0x0F, 16);
char lo = Character.forDigit(b & 0x0F, 16);
s.append(Character.toUpperCase(hi));
s.append(Character.toUpperCase(lo));
StringtoHex00String(int c)
to Hex String
String s = Integer.toHexString(c);
if (s.length() == 1) {
    return "0" + s;
} else {
    return s;
StringtoHEX1(byte b)
utility method to convert a byte to a hexadecimal string.
char[] buf = new char[2];
int j = 0;
buf[j++] = HEX_DIGITS[(b >>> 4) & 0x0F];
buf[j++] = HEX_DIGITS[b & 0x0F];
return new String(buf);
StringtoHex16(int w)
to Hex
return toHex((byte) (0xff & (w >> 8))) + toHex((byte) (0xff & w));
StringtoHex16(long l)
to Hex
String s = "000000000000000" + Long.toHexString(l);
s = s.substring(s.length() - 16);
return hexUpperCase ? s.toUpperCase() : s;
StringtoHex4(int value)
to Hex
if (value < 0)
    value &= 0xffff;
return padAddress(Integer.toHexString(value & 0x7fffffff).toUpperCase());
StringtoHex8(long x)
to Hex
String s = Long.toHexString(x);
if (s.length() % 8 != 0) {
    int r = 8 - s.length();
    for (int i = 0; i < r; i++)
        s = "0" + s;
return s;