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
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();
}
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]+");
You can check any length text with below method practically.
public static boolean isHexadecimal(String text) {
Objects.requireNonNull(text);
if(text.length() < 1)
throw new IllegalArgumentException("Text cannot be empty.");
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F' };
for (char symbol : text.toCharArray()) {
boolean found = false;
for (char hexDigit : hexDigits) {
if (symbol == hexDigit) {
found = true;
break;
}
}
if(!found)
return false;
}
return true;
}
Used this in my own code to check if string is a MAC address
boolean isHex = mac_addr.matches("^[0-9a-fA-F]+$");
My beef with the other answers provided in this thread is that if the String's length is large, it would throw an exception as well. Therefore, not very useful if you are testing if a MAC address consist of valid hexadecimals.
Don't be terrified of using regex!
Long.parseLong has a second form that takes a radix as its second argument.
private static boolean isHexNumber (String cadena) {
try {
Long.parseLong(cadena, 16);
return true;
}
catch (NumberFormatException ex) {
// Error handling code...
return false;
}
}
No-library approach
public static boolean isHexadecimal(String value)
{
if (value.startsWith("-"))
{
value = value.substring(1);
}
value = value.toLowerCase();
if (value.length() <= 2 || !value.startsWith("0x"))
{
return false;
}
for (int i = 2; i < value.length(); i++)
{
char c = value.charAt(i);
if (!(c >= '0' && c <= '9' || c >= 'a' && c <= 'f'))
{
return false;
}
}
return true;
}