Java Integer to Hex intToFixedLengthHex(int value, int length)

Here you can find the source of intToFixedLengthHex(int value, int length)

Description

Takes value and return its HEX value prefixed with zeros .

License

Open Source License

Parameter

Parameter Description
value the value to be converted
length the length to pad to.

Return

a non-null String fixed width representing the HEX of value

Declaration

public static String intToFixedLengthHex(int value, int length) 

Method Source Code

//package com.java2s;
/**/*from   w  w w .  j ava  2  s .c o m*/
 * ---<br>
 * Mailster (C) 2007-2009 De Oliveira Edouard
 * <p>
 * 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 2 of the License, or (at your option) any later
 * version.
 * <p>
 * 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.
 * <p>
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 675 Mass
 * Ave, Cambridge, MA 02139, USA.
 * <p>
 * See&nbsp; <a href="http://tedorg.free.fr/en/projects.php" target="_parent">Mailster
 * Web Site</a> <br>
 * ---
 * <p>
 * StringUtilities.java - Various methods to handle strings.
 * 
 * @author <a href="mailto:doe_wanted@yahoo.fr">Edouard De Oliveira</a>
 * @version $Revision: 1.8 $, $Date: 2009/05/17 20:56:35 $
 */

public class Main {
    /**
     * Takes <code>value</code> and return its HEX value prefixed with zeros .
     *  
     * @param value the value to be converted
     * @param length the length to pad to.
     * @return a non-null String fixed width representing the HEX of value
     */
    public static String intToFixedLengthHex(int value, int length) {
        String str = Integer.toHexString(value);
        StringBuilder paddedStr = new StringBuilder();

        if (str.length() < length) {
            for (int i = 0; i < length - str.length(); i++)
                paddedStr.append('0');
        }
        paddedStr.append(str);

        return paddedStr.toString();
    }
}

Related

  1. integerToHex(final Object value, final int desimals)
  2. integerToHex(int val)
  3. integerToHexShort(int value)
  4. integerToHexString(final int aValue, final int aFieldWidth)
  5. integerToHexString(int value, int minBytes)
  6. intToFixedLengthHexString(int i, int length)
  7. intToHex(int a)
  8. intToHex(int i)
  9. intToHex(int i)