0

I'm starting with Android Studio and making a simple App that take an input from am EditText and check if have four characters long(the easy part) and if the whole string is an hexadecimal value like "FFA1".

I think in to add the "0x" to the String but that use more resources a far a read in another post/questions. I also read that exist a library called TextUtils with a function isDigitsOnly() but I'm not sure if they call digits to 0-9 values or 0-9 and A-F too.

In VB.NET I use to add &H to the String and use isNumeric() function to detect Hexa values, but in android I'm lost.

Can somebody enlighten me? Thanks.

E_Blue
  • 1,021
  • 1
  • 20
  • 45
  • 1
    Did you type your title into google? Many answers like this: http://stackoverflow.com/questions/11424540/verify-if-string-is-hexadecimal OR http://stackoverflow.com/questions/21992023/check-hex-string-contains-only-hex-bounded-values. Why won't those work? – Rick S Nov 07 '16 at 15:26
  • __But in android I'm lost__ . You need to say **Java** instead. – Nabin Nov 07 '16 at 15:28
  • @RickS No, I use "android studio is hexadecimal" instead and get a lot of code about colors. – E_Blue Nov 07 '16 at 15:32
  • I gave you two solutions. The language you are using is java, so you should be searching for java code. Android studio is not a language. – Rick S Nov 07 '16 at 15:34
  • @NabinKhadka Probably yes but since I'm starting with Android and I'm not an expert in JAVA code I don't know if all the libraries, methods and functions are compatible between both. – E_Blue Nov 07 '16 at 15:36
  • @RickS I see some examples with regex but I never use regex and I don't want to just copy&paste, of course it works but I want to know why. I mean, since I'm learning something new I want to be able to create my own regex if I need it. Now I will make some research about regex. Thanks for your input. – E_Blue Nov 07 '16 at 15:40
  • Probably Check [this](http://stackoverflow.com/questions/5317320/regex-to-check-string-contains-only-hex-characters) – ADM Nov 07 '16 at 16:03

1 Answers1

0

You should to test with this void:

private void test(){     
     try{
String hex = "Your String"
int hexadecimalValue = Integer.parseInt(hex, 16);  
System.out.println("valid hexadecimal");
}  catch(NumberFormatException e){
// not a valid hex
System.out.println("not a valid hexadecimal");
}}
SofDroid
  • 5,450
  • 2
  • 13
  • 16