Java Integer to Hex intToFixedLengthHexString(int i, int length)

Here you can find the source of intToFixedLengthHexString(int i, int length)

Description

Converts an integer into a fixed, zero-padded hexadecimal string representation.

License

Open Source License

Parameter

Parameter Description
i The source integer to be converted.
length The length of the string to be zero-padded

Return

A zero-padded hexadecimal string of the integer, i of length length.

Declaration

public static String intToFixedLengthHexString(int i, int length) 

Method Source Code

//package com.java2s;
/*/*w  w  w.jav a 2s  . c  om*/
 * Copyright (c) 2010-2011 Brigham Young University
 * 
 * This file is part of the BYU RapidSmith Tools.
 * 
 * BYU RapidSmith Tools is free software: you may 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.
 * 
 * BYU RapidSmith Tools 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.
 * 
 * A copy of the GNU General Public License is included with the BYU 
 * RapidSmith Tools. It can be found at doc/gpl2.txt. You may also 
 * get a copy of the license at <http://www.gnu.org/licenses/>.
 * 
 */

public class Main {
    /**
     * Converts an integer into a fixed, zero-padded hexadecimal string representation.
     * @param i The source integer to be converted.
     * @param length The length of the string to be zero-padded
     * @return A zero-padded hexadecimal string of the integer, i of length length.
     */
    public static String intToFixedLengthHexString(int i, int length) {
        String s = Integer.toHexString(i);
        String pad = "0";

        while (s.length() < length) {
            s = pad.concat(s);
        }

        if (s.length() > length) {
            s = s.substring(0, length);
        }

        return s.toUpperCase();
    }

    public static String toHexString(byte b) {
        StringBuffer buf = new StringBuffer();
        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        int high = 0;
        int low = 0;

        high = ((b & 0xf0) >> 4);
        low = (b & 0x0f);
        buf.append(hexChars[high]);
        buf.append(hexChars[low]);

        return buf.toString();
    }

    public static String toHexString(byte[] block) {
        StringBuffer buf = new StringBuffer();
        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        int len = block.length;
        int high = 0;
        int low = 0;
        for (int i = 0; i < len; i++) {
            high = ((block[i] & 0xf0) >> 4);
            low = (block[i] & 0x0f);
            buf.append(hexChars[high]);
            buf.append(hexChars[low]);
        }
        return buf.toString();
    }

    public static String toHexString(int integer) {
        byte block[] = new byte[4];
        block[0] = (byte) ((integer >> 24) & 0x000000FF);
        block[1] = (byte) ((integer >> 16) & 0x000000FF);
        block[2] = (byte) ((integer >> 8) & 0x000000FF);
        block[3] = (byte) (integer & 0x000000FF);

        StringBuffer buf = new StringBuffer();
        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        int len = block.length;
        int high = 0;
        int low = 0;
        for (int i = 0; i < len; i++) {
            high = ((block[i] & 0xf0) >> 4);
            low = (block[i] & 0x0f);
            buf.append(hexChars[high]);
            buf.append(hexChars[low]);
        }
        return buf.toString();
    }

    /**
     * This changes a normal string into the hexadecimal string equivalent.  
     * It will add two hexadecimal characters for each character of the string.
     * @param s The string to be converted
     * @return The string s converted to hexadecimal
     */
    public static String toHexString(String s) {
        char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        StringBuffer sb = new StringBuffer(s.length() * 2);
        for (int i = 0; i < s.length(); i++) {
            sb.append(hexChar[(s.charAt(i) & 0xf0) >>> 4]);
            sb.append(hexChar[(s.charAt(i) & 0x0f)]);
        }

        return sb.toString();
    }
}

Related

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