Android Hex String Create isHexChar(String hexString, boolean checkSpaceFlag)

Here you can find the source of isHexChar(String hexString, boolean checkSpaceFlag)

Description

Method Check String

License

Open Source License

Parameter

Parameter Description
The string to be format.

Declaration

public static boolean isHexChar(String hexString, boolean checkSpaceFlag) 

Method Source Code

//package com.java2s;

public class Main {
    /**// w  w  w  . ja  v  a  2s .com
     * 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(char ch)
  3. isHexChar(String hexString)
  4. toHexChar(int nibble)
  5. appendHex(final StringBuffer buf, final byte i)
  6. appendHex(final StringBuffer buf, final int i)