In terms of programmer time, it is probably best to name a string-chain parsing function (e.g. Java Integer.parseInt (str, base)) and see if you get an exception. If you want to write it yourself and potentially have more time / space-efficiency ...
The most effective, I assume, will be a lookup table for each character. You will have a 2 ^ 8 (or 2 ^ 16 for Unicode) -entry array of booleans, each of which will be true if it is a valid hexadecimal character, or false if not. The code will look something like this (in Java, sorry ;-):
boolean lut[256]={false,false,true,........} boolean OnlyHexInString(String text) { for(int i = 0; i < text.size(); i++) if(!lut[text.charAt(i)]) return false; return true; }
Matt J Oct 21 '08 at 10:53
source share