Java Integer to Hex int2HexChars(final int data)

Here you can find the source of int2HexChars(final int data)

Description

Method to generate the hexadecimal character presentation of an integer This call is equivalent to HexUtils#int2HexChars(int,char[],int) with parameters (data, null, 0)

License

Open Source License

Parameter

Parameter Description
data integer to generate the hexadecimal character presentation from

Return

new char array with 8 elements

Declaration

public static char[] int2HexChars(final int data) 

Method Source Code

//package com.java2s;
/*//from w  ww.  ja  v  a  2 s. c  o  m
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    private static final char[] _NIBBLE_CHAR_LOOKUP = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
            'C', 'D', 'E', 'F' };

    /**
     * Method to generate the hexadecimal character presentation of an integer This call is equivalent to {@link HexUtils#int2HexChars(int, char[], int)} with parameters (data, null, 0)
     * @param data integer to generate the hexadecimal character presentation from
     * @return new char array with 8 elements
     */
    public static char[] int2HexChars(final int data) {
        return int2HexChars(data, new char[8], 0);
    }

    /**
     * Method to generate the hexadecimal character presentation of an integer
     * @param data integer to generate the hexadecimal character presentation from
     * @param dstHexChars the char array the hexadecimal character presentation should be copied to, if this is null, dstOffset is ignored and a new char array with 8 elements is created
     * @param dstOffset offset at which the hexadecimal character presentation is copied to dstHexChars
     * @return the char array the hexadecimal character presentation was copied to
     */
    public static char[] int2HexChars(final int data, char[] dstHexChars, int dstOffset) {
        if (dstHexChars == null) {
            dstHexChars = new char[8];
            dstOffset = 0;
        }

        b2HexChars((byte) ((data & 0xFF000000) >> 24), dstHexChars, dstOffset);
        b2HexChars((byte) ((data & 0x00FF0000) >> 16), dstHexChars, dstOffset + 2);
        b2HexChars((byte) ((data & 0x0000FF00) >> 8), dstHexChars, dstOffset + 4);
        b2HexChars((byte) (data & 0x000000FF), dstHexChars, dstOffset + 6);
        return dstHexChars;
    }

    /**
     * Method to generate the hexadecimal character presentation of a byte<br>
     * This call is equivalent to {@link HexUtils#b2HexChars(byte, char[], int)} with parameters (data, null, 0)
     * @param data byte to generate the hexadecimal character presentation from
     * @return a new char array with exactly 2 elements
     */
    public static char[] b2HexChars(final byte data) {
        return b2HexChars(data, null, 0);
    }

    /**
     * Method to generate the hexadecimal character presentation of a byte
     * @param data byte to generate the hexadecimal character presentation from
     * @param dstHexChars the char array the hexadecimal character presentation should be copied to, if this is null, dstOffset is ignored and a new char array with 2 elements is created
     * @param dstOffset offset at which the hexadecimal character presentation is copied to dstHexChars
     * @return the char array the hexadecimal character presentation was copied to
     */
    public static char[] b2HexChars(final byte data, char[] dstHexChars, int dstOffset) {
        if (dstHexChars == null) {
            dstHexChars = new char[2];
            dstOffset = 0;
        }

        // /////////////////////////////
        // NIBBLE LOOKUP
        dstHexChars[dstOffset] = _NIBBLE_CHAR_LOOKUP[(data & 0xF0) >> 4];
        dstHexChars[dstOffset + 1] = _NIBBLE_CHAR_LOOKUP[data & 0x0F];

        return dstHexChars;
    }
}

Related

  1. int2hex(final int i)
  2. int2hex(int a, StringBuffer str)
  3. int2hex(int i)
  4. int2hex(int i)
  5. int2HexChar(int val)
  6. int2HexString(final int i)
  7. int2HexString(int n)
  8. int2hexString(int x)
  9. int2HexString(StringBuilder buf, int value)