Java - Write code to Return the integer value of the passed-in char interpreted as a hex digit.

Requirements

Write code to Return the integer value of the passed-in char interpreted as a hex digit.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        char c = 'a';
        System.out.println(hexcharToInt(c));
    }//from   ww  w  .j  a  v  a 2 s  .  c o m

    /**
     * Return the integer value of the passed-in char interpreted
     * as a hex digit.
     * 
     * FIXME: return -1 on bad conversion -- HR.
     */

    public static int hexcharToInt(char c) {
        return c - (c > '9' ? (c >= 'a' ? 'a' - 10 : 'A' - 10) : '0');
    }
}