Here you can find the source of toHexString(int input)
Parameter | Description |
---|---|
input | Input to convert |
public static String toHexString(int input)
//package com.java2s; /*//from www.ja v a 2 s. c o m * File: HashFunctionUtil.java * Authors: Kevin R. Dixon * Company: Sandia National Laboratories * Project: Cognitive Foundry * * Copyright Jan 26, 2011, Sandia Corporation. * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive * license for use of this work by or on behalf of the U.S. Government. * Export of this program may require a license from the United States * Government. See CopyrightHistory.txt for complete details. * */ public class Main { /** * Char table for convenience */ static final char[] HEX_CHAR_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /** * Converts the given int to a Big Endian hex string * @param input * Input to convert * @return * hex string (8 chars) */ public static String toHexString(int input) { // Java's Integer.toHexString doesn't put the leading zero if needed, // so we have to do it ourselves... sigh. char[] hex = new char[8]; int index = 0; for (int i = 0; i < 4; i++) { // If we didn't do the 24-8*i, then we'd end up with a // Little Endian result... gross. final int b = (input >>> (24 - 8 * i)) & 0xFF; hex[index++] = HEX_CHAR_TABLE[b >>> 4]; hex[index++] = HEX_CHAR_TABLE[b & 0xF]; } String result = null; try { result = new String(hex); } catch (Exception e) { throw new RuntimeException(e); } return result; } /** * Converts the given long to a Big Endian hex string * @param input * Input to convert * @return * hex string (16 chars) */ public static String toHexString(long input) { // Java's Integer.toHexString doesn't put the leading zero if needed, // so we have to do it ourselves... sigh. char[] hex = new char[16]; int index = 0; for (int i = 0; i < 8; i++) { // If we didn't do the 24-8*i, then we'd end up with a // Little Endian result... gross. final int b = (int) ((input >>> (56 - 8 * i)) & 0xFF); hex[index++] = HEX_CHAR_TABLE[b >>> 4]; hex[index++] = HEX_CHAR_TABLE[b & 0xF]; } String result = null; try { result = new String(hex); } catch (Exception e) { throw new RuntimeException(e); } return result; } /** * Converts a byte array to a lower-case Hex string * @param bytes * byte array to convert * @return * String representing the byte array */ public static String toHexString(byte[] bytes) { char[] hex = new char[2 * bytes.length]; int index = 0; for (int i = 0; i < bytes.length; i++) { final int b = (bytes[i] & 0xFF); hex[index++] = HEX_CHAR_TABLE[b >>> 4]; hex[index++] = HEX_CHAR_TABLE[b & 0xF]; } String result = null; try { result = new String(hex); } catch (Exception e) { throw new RuntimeException(e); } return result; } }