Java Integer to Hex intToHexString(int i)

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

Description

int To Hex String

License

Academic Free License

Return

a 32-bit integer to a hex string, but guarantee that the string is eight chars long by padding with leading zeros

Declaration

public static String intToHexString(int i) 

Method Source Code

//package com.java2s;
// Licensed under the Academic Free License version 3.0

public class Main {
    /**//from   w ww .j av a 2s  .c o m
     * @return a 32-bit integer to a hex string, but guarantee that
     *  the string is eight chars long by padding with leading zeros
     */
    public static String intToHexString(int i) {
        return intToHexString(i, 8);
    }

    /**
     * @return a 32-bit integer to a hex string, but guarantee that
     *  the string is <code>minChars</code> chars long by padding with leading zeros
     */
    public static String intToHexString(int i, int minChars) {
        if ((minChars < 1) || (minChars > 8))
            minChars = 8;
        String s = Integer.toHexString(i);
        while (s.length() < minChars)
            s = "0" + s;
        return s;
    }

    /**
     * Return hex string of bytes.
     */
    public static String toHexString(byte[] b) {
        StringBuffer s = new StringBuffer();
        for (int i = 0; i < b.length; ++i)
            s.append(byteToHexString(b[i]));
        return s.toString();
    }

    /**
     * @return byte to two character hex string.
     */
    public static String byteToHexString(int b) {
        String s = Integer.toHexString(b & 0xFF);

        if (s.length() == 1)
            return "0" + s;
        else
            return s;
    }
}

Related

  1. intToHexStr(int i, int len)
  2. intToHexStr(int src, int len, int code)
  3. IntToHexString(final int value)
  4. intToHexString(int _i)
  5. intToHexString(int i)
  6. intToHexString(int i)
  7. intToHexString(int num)
  8. intToHexString(int number)
  9. intToHexString(int val, int width)