Format int To Hex String - Java java.lang

Java examples for java.lang:Hex

Description

Format int To Hex String

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int val = 2;
        int width = 2;
        System.out.println(intToHexString(val, width));
    }//www . j  a  va 2  s.  c  om

    static String mZeroes = "00000000000000000000000000000000";

    public static String intToHexString(int val, int width) {
        String str = Integer.toHexString(val);
        if (str.length() >= width) {
            return str;
        }
        return mZeroes.substring(0, width - str.length()) + str;
    }
}

Related Tutorials