I have about 10,000 hexadecimal numbers (32 digits each) in an excel file, in one column. I want to verify that all of them are indeed hexadecimal number and that no other characters are present. My first thought was to import them into a list and then apply 16^^# to the list and see if I get any errors. This doesn't seem to work because the import is bringing them in as strings. I can't figure out how to convert the strings to something that 16^^ will work on. Is there something I can do?

Another approach, which I didn't try would be using regular expressions to validate that only proper characters are in each number. I almost never use regular expressions, so a hint along those lines would be helpful.

If there's a better approach I would love to hear it.

I'm using Windows 7, Mathematica, Excel 10.

  • $\begingroup$ Are you on Windows or Mac/Linux? $\endgroup$ Commented Feb 18, 2015 at 22:06
  • $\begingroup$ @2012rcampion I'm using Windows 7. I just added that to my original question. $\endgroup$ Commented Feb 18, 2015 at 22:20
  • $\begingroup$ Assuming the "characters" in the imported strings are just the standard hex, StringMatchQ[#, Characters["0123456789aAbBcCdDeEfF"] ..] & /@myimportedstrings will give the vector of good/bad, and And @@ (StringMatchQ[#, Characters["0123456789aAbBcCdDeEfF"] ..] & /@ myimportedstrings) will give a go/no go for whole set. $\endgroup$
    – ciao
    Commented Feb 18, 2015 at 22:37
  • $\begingroup$ If you're doing most of your processing in Excel, try using ISERROR(HEX2DEC(cell)). If you're importing into Mathematica anyway, use one of the StringMatchQ[#,HexadecimalCharacter..]& solutions below. $\endgroup$ Commented Feb 18, 2015 at 23:23
  • $\begingroup$ @2012rcampion I tried HEX2DEC, but my probable Hex numbers were too long. Thanks. $\endgroup$ Commented Feb 19, 2015 at 3:22

1 Answer 1


You can use StringMatchQ and HexadecimalCharacter to check the list for non-hex items.

First I'll make some hexadecimal strings and insert some non-hex ones.

hexStrings = IntegerString[RandomInteger[{10, 30000}, 100], 16];
AppendTo[hexStrings, "zxc2"]; PrependTo[hexStrings, "x34c"];

Now we can Map the StringMatchQ function over the list using a Repeated pattern of HexadecimalCharacter. The operator form of Position is then applied to this result to get the False matches. Finally it is Flattened to get a flat list of these positions.

  Position[False]@(StringMatchQ[#, HexadecimalCharacter ..] & /@ hexStrings)

This will return the positions of any non-hex strings in the list or an empty list if all strings are hexadecimal. Once you have the positions you can use other functions to manage the non-hex strings.

Hope this helps.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.