Java Hex Calculate toHex(int value, int length)

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

Description

to Hex

License

Open Source License

Declaration

public static String toHex(int value, int length) 

Method Source Code

//package com.java2s;
/****************************************************************************
 This file is part of TIImageTool.//w  ww  . j ava  2s.  c o m

 TIImageTool 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 3 of the License, or
 (at your option) any later version.

 TIImageTool 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.

 You should have received a copy of the GNU General Public License
 along with TIImageTool.  If not, see <http://www.gnu.org/licenses/>.
    
 Copyright 2011 Michael Zapf
 www.mizapf.de
    
 ****************************************************************************/

public class Main {
    public static String toHex(int value, int length) {
        return toHex(value, length, false);
    }

    public static String toHex(int value, int length, boolean bUppercase) {
        String HEX = "0123456789abcdef0123456789ABCDEF";
        char[] out = new char[length];
        int offset = bUppercase ? 16 : 0;

        for (int i = 0; i < length; i++) {
            out[length - i - 1] = HEX.charAt(offset + (value & 0x0f));
            value >>= 4;
        }
        return new String(out);
    }
}

Related

  1. toHex(int val)
  2. toHEX(int value)
  3. toHex(int value)
  4. toHex(int value)
  5. toHex(int value, int bits)
  6. toHex(int value, int length)
  7. toHex(int value, int minNumOfDigits)
  8. toHex(long i)
  9. toHex(long i, int r0, boolean r1)