Return true if the argument string seems to be a Hex data string, like "a0 13 2f ". - Java java.lang

Java examples for java.lang:String Format

Description

Return true if the argument string seems to be a Hex data string, like "a0 13 2f ".

Demo Code


//package com.java2s;

public class Main {
    /**//from  w ww  .j a va 2 s  . com
     * Return true if the argument string seems to be a
     * Hex data string, like "a0 13 2f ".  Whitespace is
     * ignored.
     */
    public static final boolean isHex(String sampleData) {
        for (int i = 0; i < sampleData.length(); i++) {
            if (!isHexStringChar(sampleData.charAt(i)))
                return false;
        }
        return true;
    }

    /**
     * Return true if the input argument character is
     * a digit, a space, or A-F.
     */
    public static final boolean isHexStringChar(char c) {
        return (Character.isDigit(c) || Character.isWhitespace(c) || (("0123456789abcdefABCDEF"
                .indexOf(c)) >= 0));
    }
}

Related Tutorials