Java Hex Calculate toHexString(int bits, int value)

Here you can find the source of toHexString(int bits, int value)

Description

to Hex String

License

Open Source License

Declaration

public static String toHexString(int bits, int value) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * This file is part of logisim-evolution.
 *
 *   logisim-evolution 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.
 *
 *   logisim-evolution 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 logisim-evolution.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   Original code by Carl Burch (http://www.cburch.com), 2011.
 *   Subsequent modifications by ://  w  w w .jav a  2  s.  c  o m
 *     + Haute ?cole Sp?cialis?e Bernoise
 *       http://www.bfh.ch
 *     + Haute ?cole du paysage, d'ing?nierie et d'architecture de Gen?ve
 *       http://hepia.hesge.ch/
 *     + Haute ?cole d'Ing?nierie et de Gestion du Canton de Vaud
 *       http://www.heig-vd.ch/
 *   The project is currently maintained by :
 *     + REDS Institute - HEIG-VD
 *       Yverdon-les-Bains, Switzerland
 *       http://reds.heig-vd.ch
 *******************************************************************************/

public class Main {
    public static String toHexString(int bits, int value) {
        if (bits < 32)
            value &= (1 << bits) - 1;
        String ret = Integer.toHexString(value);
        int len = (bits + 3) / 4;
        while (ret.length() < len)
            ret = "0" + ret;
        if (ret.length() > len)
            ret = ret.substring(ret.length() - len);
        return ret;
    }
}

Related

  1. toHexString(final int i)
  2. toHexString(final int value)
  3. toHexString(final long num, final char paddingChar, int min, int max)
  4. toHexString(int b)
  5. toHexString(int b)
  6. toHexString(int decimal, int stringLength)
  7. toHexString(int decimal, int stringLength)
  8. toHexString(int i)
  9. toHexString(int i)