Java - Write code to Convert the given character to a four-digit hexadecimal string

Requirements

Write code to Convert the given character to a four-digit hexadecimal string

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        char c = 'a';
        System.out.println(hex4(c));
    }/*www  . jav  a  2 s . c  o m*/

    /**
     * Hexadecimal characters
     */
    private final static char[] HEX_DIGITS = { '0', '1', '2', '3', '4',
            '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    /**
     * Converts the given character to a four-digit hexadecimal string
     * @param c the character to convert
     * @return the string
     */
    private static String hex4(char c) {
        char[] r = new char[] { '0', '0', '0', '0' };
        int i = 3;
        while (c > 0) {
            r[i] = HEX_DIGITS[c & 0xF];
            c >>>= 4;
            --i;
        }
        return new String(r);
    }
}