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(int val)
Quick conversion to hex string.
return toHexString(val, 4);
StringtoHexString(int val, int minLength)
to Hex String
StringBuilder sb = new StringBuilder();
while (val != 0) {
    switch (val & 0xf) {
    case 0x0:
        sb.append('0');
        break;
    case 0x1:
        sb.append('1');
...
StringtoHexString(int value, int len)
Returns Hexadecimal String of specified number of digits padding with zeroes if necessary
String retval = "";
for (int i = (len - 1) * 4; i >= 0; i -= 4) {
    int j = value >> i & 0xF;
    if (j > 9) {
        j += LOWERCASE_ASCII_MIN - 10; 
    } else {
        j += NUMBER_ASCII_MIN; 
    retval += (char) j; 
return retval;
StringToHexString(int value, int length)
Format an integer to its hex representaion, with (at least) the given number of digits, left padded with '0'
String answer = Integer.toHexString(value).toUpperCase();
int currentLength = answer.length();
if (currentLength < length) {
    answer = Zeroes.substring(0, length - currentLength) + answer;
return answer;
StringtoHexString(int value, int minLength)
to Hex String
StringBuilder sb = new StringBuilder(Integer.toHexString(value).toUpperCase());
while (sb.length() < minLength) {
    sb.insert(0, '0');
return sb.toString();
StringtoHexString(int value, int numDigits)
Returns a base-10 integer as a hexadecimal number in String.
StringBuilder result = new StringBuilder();
for (int i = (numDigits - 1); i >= 0; i--) {
    String digit = toHexDigit(value, i);
    result.append(digit);
return result.toString().toUpperCase();
StringtoHexString(int[] data)
to Hex String
if (data == null)
    return "null";
StringBuilder stringBuilder = new StringBuilder(data.length * 5);
stringBuilder.append('[');
for (int i = 0; i < data.length; i++) {
    stringBuilder.append(String.format("0x%02X", data[i]));
    if (i + 1 < data.length)
        stringBuilder.append(", ");
...
StringtoHexString(int[] ia)
Get the hex string for the bytes.
return toHexString(ia, 0, ia.length, 0);
StringtoHexString(int[] x, int bitsPerDigit)
to Hex String
final int digitsPerLong = (59 / bitsPerDigit) + 1;
final StringBuilder b = new StringBuilder();
for (int i = 0; i < x.length;) {
    long a = 0;
    int j = 0;
    for (; i < x.length && j < digitsPerLong; j++) {
        a |= ((long) x[i] << (bitsPerDigit * j));
        i++;
...
StringtoHexString(Integer num)
to Hex String
String hex = Integer.toHexString(num.intValue());
switch (hex.length()) {
case 0:
    hex = "00000000";
    break;
case 1:
    hex = "0000000" + hex;
    break;
...