Converts a single char hex value to its corresponding int. - Java java.lang

Java examples for java.lang:String Hex

Description

Converts a single char hex value to its corresponding int.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        char hex = 'a';
        System.out.println(hexToInt(hex));
    }/*  w  ww.  ja v  a  2  s  . c om*/

    /**
     * Converts a single char hex value to its corresponding int.
     */
    public static int hexToInt(final char hex) {
        // intToHex "%" + hex.charAt( c / 16 ) + hex.charAt( c % 16 )
        if (hex >= '0' && hex <= '9')
            return hex - 48;

        char dehex = hex;

        // If it's the wrong case...
        if (dehex >= 'a' && dehex <= 'z')
            return dehex - 16 - 'G';

        return 16 + (dehex - 'G');
    }
}

Related Tutorials