Here you can find the source of intToFixedLengthHex(int value, int length)
value
and return its HEX value prefixed with zeros .
Parameter | Description |
---|---|
value | the value to be converted |
length | the length to pad to. |
public static String intToFixedLengthHex(int value, int length)
//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 <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(); } }