Android Hex String Create isHexChar(char ch)

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

Description

Return true if the string is HexChars(1234567890abcdefABCDEF).

License

Open Source License

Declaration

public static byte isHexChar(char ch) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from   w  w  w.  ja  v  a  2  s.  c o m
     * Return true if the string is HexChars(1234567890abcdefABCDEF).
     *
     */
    public static byte isHexChar(char ch) {
        if ('a' <= ch && ch <= 'f')
            return (byte) (ch - 'a' + 10);
        if ('A' <= ch && ch <= 'F')
            return (byte) (ch - 'A' + 10);
        if ('0' <= ch && ch <= '9')
            return (byte) (ch - '0');

        return -1;
    }

    /**
     * Method Check String 
     *
     * @param The string to be format.
     * 
     * @param checkSpaceFlag=false: skip the space.
     *
     */
    public static boolean isHexChar(String hexString, boolean checkSpaceFlag) {
        if (null == hexString || 0 == hexString.length())
            return false;

        int hexLen = hexString.length();
        int hexCharCount = 0;
        char ch;
        for (int i = 0; i < hexLen; i++) {
            ch = hexString.charAt(i);
            if (ch == ' ') {
                if (checkSpaceFlag)
                    return false;
            } else {
                if (isHexChar(ch) < 0)
                    return false;
                else
                    hexCharCount++;
            }
        }

        if (hexCharCount % 2 != 0)
            return false;

        return true;
    }

    /**
     * Method Check String 
     *
     * @param The string to be format.
     *
     */
    public static boolean isHexChar(String hexString) {
        return isHexChar(hexString, true);
    }
}

Related

  1. appendHex(StringBuffer sb, byte b)
  2. isHexChar(String hexString, boolean checkSpaceFlag)
  3. isHexChar(String hexString)
  4. toHexChar(int nibble)
  5. appendHex(final StringBuffer buf, final byte i)