Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify if String is hexadecimal

Tags:

java

string

hex

I have a String like "09a" and I need a method to confirm if the text is hexadecimal. The code I've posted does something similar, it verifies that a string is a decimal number. I want to do the same, but for hexadecimal.

    private static boolean isNumeric(String cadena) {
    try {
        Long.parseLong(cadena);
        return true;
    } catch (NumberFormatException nfe) {
        JOptionPane.showMessageDialog(null,"Uno de los números, excede su capacidad.");
        return false;
    }
}
like image 709
Axel Vazquez Avatar asked Jul 11 '12 02:07

Axel Vazquez


People also ask

How do you know if a string is hexadecimal?

If you want to check if the input is in hexadecimal format, you shouldn't trim it at the beginning. Also, your loop could fail faster if you just return false on the first occurrence of a non-hex character (there's probably some method to check for containment so you could do: if (! hexDigits.

Why is hex a string python?

Python hex() function is used to convert an integer to a lowercase hexadecimal string prefixed with “0x”. We can also pass an object to hex() function, in that case the object must have __index__() function defined that returns integer. The input integer argument can be in any base such as binary, octal etc.


2 Answers

There's an overloaded Long.parseLong that accepts a second parameter, specifying the radix:

Long.parseLong(cadena,16); 

As an alternative, you could iterate over the characters in the string and call Character.digit(c,16) on them (if any of them return -1 it's not a valid hexadecimal digit). This is especially useful if the string is too large to fit in a long (as pointed out in the comments, that would cause an exception if the first method is used). Example:

private static boolean isNumeric(String cadena) {     if ( cadena.length() == 0 ||           (cadena.charAt(0) != '-' && Character.digit(cadena.charAt(0), 16) == -1))         return false;     if ( cadena.length() == 1 && cadena.charAt(0) == '-' )         return false;      for ( int i = 1 ; i < cadena.length() ; i++ )         if ( Character.digit(cadena.charAt(i), 16) == -1 )             return false;     return true; } 

BTW, I'd suggest separating the concerns of "testing for a valid number" and "displaying a message to the user", that's why I simply returned false in the example above instead of notifying the user first.

Finally, you could simply use a regular expression:

cadena.matches("-?[0-9a-fA-F]+"); 
like image 186
mgibsonbr Avatar answered Sep 21 '22 19:09

mgibsonbr


Horrible abuse of exceptions. Don't ever do this! (It's not me, it's Josh Bloch's Effective Java). Anyway, I suggest

private static final Pattern HEXADECIMAL_PATTERN = Pattern.compile("\\p{XDigit}+");

private boolean isHexadecimal(String input) {
    final Matcher matcher = HEXADECIMAL_PATTERN.matcher(input);
    return matcher.matches();
}
like image 25
Evgeniy Dorofeev Avatar answered Sep 24 '22 19:09

Evgeniy Dorofeev