Java Hex Convert To fromHexChar(char ch)

Here you can find the source of fromHexChar(char ch)

Description

Convert the given hex character (0-9, A-Z, or a-z) into its decimal equivalent value.

License

Apache License

Parameter

Parameter Description
ch A hex character.

Return

Decimal equivalent of value (0-15).

Declaration

public static int fromHexChar(char ch) 

Method Source Code

//package com.java2s;
/*/*ww  w .  j  av  a 2s .c  o m*/
 * Copyright (C) 2014 Dell, Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Convert the given hex character (0-9, A-Z, or a-z) into its decimal equivalent value.
     * 
     * @param  ch   A hex character.
     * @return      Decimal equivalent of value (0-15).
     */
    public static int fromHexChar(char ch) {
        switch (ch) {
        case '0':
            return 0;
        case '1':
            return 1;
        case '2':
            return 2;
        case '3':
            return 3;
        case '4':
            return 4;
        case '5':
            return 5;
        case '6':
            return 6;
        case '7':
            return 7;
        case '8':
            return 8;
        case '9':
            return 9;
        case 'a':
        case 'A':
            return 10;
        case 'b':
        case 'B':
            return 11;
        case 'c':
        case 'C':
            return 12;
        case 'd':
        case 'D':
            return 13;
        case 'e':
        case 'E':
            return 14;
        case 'f':
        case 'F':
            return 15;
        default:
            throw new IllegalArgumentException("Must be a hex char: " + ch);
        }
    }
}

Related

  1. fromHex(String[] data)
  2. fromHex2B(String src)
  3. fromHex8B(String src)
  4. fromHexa(String s)
  5. fromHexBinary(String s)
  6. fromHexChars(char[] chars, int i)
  7. fromHexDigest(String hexDigest)
  8. fromHexDigit(final char c)
  9. fromHexDigit(int c)