Java Hex Calculate toHexString(int val)

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

Description

Quick conversion to hex string.

License

Open Source License

Parameter

Parameter Description
val The value to convert.

Declaration

public static String toHexString(int val) 

Method Source Code

//package com.java2s;
/**//w ww .j a  v a2  s  .  c  o  m
 * This file is protected by Copyright. Please refer to the COPYRIGHT file
 * distributed with this source distribution.
 *
 * This file is part of REDHAWK.
 *
 * REDHAWK is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * REDHAWK 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/.
 */

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

    /** Quick conversion to hex string. Unlike {@link Integer#toHexString} this returns a string
     *  that is in upper-case and padded with leading zeros.
     *  @param val The value to convert.
     */
    public static String toHexString(int val) {
        return toHexString(val, 4);
    }

    /** Quick conversion to hex string. Unlike {@link Integer#toHexString} this returns a string
     *  that is in upper-case and padded with leading zeros.
     *  @param val The value to convert.
     */
    public static String toHexString(long val) {
        return toHexString(val, 8);
    }

    /** Quick conversion to hex string. Unlike {@link Integer#toHexString} this returns a string
     *  that is in upper-case and padded with leading zeros.
     *  @param val   The value to convert.
     *  @param bytes The number of bytes to consider.
     */
    public static String toHexString(int val, int bytes) {
        char[] chars = new char[bytes * 2];

        for (int i = chars.length - 1; i >= 0; i--) {
            chars[i] = hexChar[val & 0xF];
            val = val >>> 4;
        }
        return new String(chars);
    }

    /** Quick conversion to hex string. Unlike {@link Integer#toHexString} this returns a string
     *  that is in upper-case and padded with leading zeros.
     *  @param val   The value to convert.
     *  @param bytes The number of bytes to consider.
     */
    public static String toHexString(long val, int bytes) {
        char[] chars = new char[bytes * 2];

        for (int i = chars.length - 1; i >= 0; i--) {
            chars[i] = hexChar[(int) (val & 0xF)];
            val = val >>> 4;
        }
        return new String(chars);
    }
}

Related

  1. toHexString(int input)
  2. toHexString(int iValue)
  3. toHexString(int n)
  4. toHexString(int number, int digit)
  5. toHexString(int r, int g, int b, int a)
  6. toHexString(int val, int minLength)
  7. toHexString(int value, int len)
  8. ToHexString(int value, int length)
  9. toHexString(int value, int minLength)