|more useful options|
Input validation for built-in hex functionby toolic (Bishop)
|on Aug 30, 2012 at 17:04 UTC||Need Help??|
I had a bug in my code which was a result of insufficient validation of a string passed to the built-in hex function.
In my opinion, hex is much too forgiving. As shown in its documentation, it allows for a string to have a leading 0x; for example, it converts 0xC to 12. Although x is not a legal hexadecimal character, it is customary to denote a hex value with the 0xC prefix. This is quite reasonable.
Although not mentioned in the documentation, it also allows for a string to have a leading x; for example, it converts xC to 12. I guess that's reasonable, too.
Passing it an illegal hex string like t does generate a warning, as desired:
I think passing a lone x should generate a warning message (with warnings enabled), but I'm not sure it's worth submitting a perlbug since I really need to do some checking before passing a string to hex anyway.
Here is a wrapper function I decided to use. It calls hex after performing some input validation:
Another approach is to override hex.