Here you can find the source of toHexString(int val)
Parameter | Description |
---|---|
val | The value to convert. |
public static String toHexString(int val)
//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); } }