WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Strings

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Determine Hex String

 Keywords: String Hex IsHex Hexadecimal ChrHexToString StrTypeInfo

Question:

How would you check if a string is a Hexadecimal value?

Answer:

Depends. The string could be series of hex encoded bytes as a string where we require each byte to be represented by 2 hex digits. Or the input could be stream of hex digits that could represent a byte, word, long word or long long word where the size depends on how many hex digits are present, with a leading zero assumed to exist at the beginning of the string if it contains an odd number of digits? Also, does the string have a radix prefix of "0x" or "0X" at the beginning of the string? THe code would differ based on the response to these questions.

However, my first thought is to use the ChrHexToString function to handle the hex validation. Here is an undebugged code sample:

#DefineFunction IsHex (value)
   ErrorMode(@OFF)
   ChrHexToString(value)
   ErrorMode(@CANCEL)
   If LastError() == 0 Then result = 1
   Else result = 0
   Return result
#EndFunction

value = '48656C6C6F' ;returns 1
;value = '48656C6CF'  ;returns 0
;value = '48'         ;returns 1
;value = '4'          ;returns 0
;value = '?'          ;returns 0
ret = IsHex (value)
Pause('IsHex?',ret)
Exit

Also, perhaps look at command StrTypeInfo and flag 128 (C1_XDIGIT) Hexadecimal digits.


Article ID:   W18288
Filename:   Determine Hex String.txt
File Created: 2013:03:25:11:38:50
Last Updated: 2013:03:25:11:38:50