Java Hex Calculate toHexString(int i)

Here you can find the source of toHexString(int i)

Description

to Hex String

License

Open Source License

Declaration

public static String toHexString(int i) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static final char[] _HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',
            'd', 'e', 'f' };

    public static String toHexString(int i) {
        char[] buffer = new char[8];

        int index = 8;

        do {//  w w w.  j a v a2 s . c  o m
            buffer[--index] = _HEX_DIGITS[i & 15];

            i >>>= 4;
        } while (i != 0);

        return new String(buffer, index, 8 - index);
    }

    public static String toHexString(long l) {
        char[] buffer = new char[16];

        int index = 16;

        do {
            buffer[--index] = _HEX_DIGITS[(int) (l & 15)];

            l >>>= 4;
        } while (l != 0);

        return new String(buffer, index, 16 - index);
    }

    public static String toHexString(Object obj) {
        if (obj instanceof Integer) {
            return toHexString(((Integer) obj).intValue());
        } else if (obj instanceof Long) {
            return toHexString(((Long) obj).longValue());
        } else {
            return String.valueOf(obj);
        }
    }

    /**
     * Returns the string value of the object.
     * 
     * @param obj the object whose string value is to be returned
     * @return the string value of the object
     * @see {@link String#valueOf(Object obj)}
     */
    public static String valueOf(Object obj) {
        return String.valueOf(obj);
    }
}

Related

  1. toHexString(int decimal, int stringLength)
  2. toHexString(int decimal, int stringLength)
  3. toHexString(int i)
  4. toHexString(int i)
  5. toHexString(int i)
  6. toHexString(int i)
  7. toHexString(int i)
  8. toHexString(int i, int digits)
  9. toHexString(int input)