WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: mueer01 on November 24, 2025, 12:23:06 AM

Title: IsAscii( )
Post by: mueer01 on November 24, 2025, 12:23:06 AM
Hello,

is there any function to check, if a string contains ASCII Chars (a-z) only?
Something like

ret = IsAscii(buf)

e.g.:
"abcdef" ---> @TRUE
"abc-ef" ---> @FALSE

Title: Re: IsAscii( )
Post by: JTaylor on November 24, 2025, 08:04:28 AM
To be a bit pedantic but clear, ASCII characters are more than a-z.   I do not believe there is a function that will do what you require.  Here is one option.  It only does the lower case as you have indicated.  If you want upper you should be able to adjust.   You could also use StrClean() and compare sizes as another option.

Jim



#DefineFunction IsASCII(str)
  arr = ArrayFromStr(str)
  ForEach a in arr
    If Char2Num(a) < 97 || Char2Num(a) > 122 Then Return @FALSE
  Next
  Return @TRUE
#EndFunction

str = "abcd"
Message("ASCII?",IsASCII(str))

str = "Abcd"
Message("ASCII?",IsASCII(str))

str = "ab,d"
Message("ASCII?",IsASCII(str))
Title: Re: IsAscii( )
Post by: td on November 24, 2025, 10:53:48 AM
It isn't as intuitive as Jim's example but you can also use the StrTypeInfo function.

https://docs.winbatch.com/mergedProjects/WindowsInterfaceLanguage/html/WILLZ_S__050.htm

str = 'abcDe'
Type = StrTypeInfo(str, 1) ^ 128 ; Remove the hex digit bit.
; Allow both upper and lower case apha characters.
if type == 771 || type == 770 then Text = "is ASCII"
else text = "is not all ASCII"

Message(str, text)

Title: Re: IsAscii( )
Post by: spl on November 24, 2025, 11:36:14 AM
There is also sillycode
gosub udfs
ascii = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
str = "abcd"
isascii()
str = "Abcd"
isascii()
str = "ab,d"
isascii()

:udfs
#DefineSubRoutine isascii()
n = StrLen(str)
ret= "is Ascii"
For j = 1 To n
    if StrIndex(ascii,StrUpper(StrSub(str,j,1)),1,@FWDSCAN)==0 Then ret="is not Ascii"
Next
Message(str,ret)

#EndSubRoutine

Return

Title: Re: IsAscii( )
Post by: JTaylor on November 24, 2025, 12:35:24 PM
Nice.  Not sure I recall seeing that function before.  Thanks.

Jim

Quote from: td on November 24, 2025, 10:53:48 AMIt isn't as intuitive as Jim's example but you can also use the StrTypeInfo function.

https://docs.winbatch.com/mergedProjects/WindowsInterfaceLanguage/html/WILLZ_S__050.htm

str = 'abcDe'
Type = StrTypeInfo(str, 1) ^ 128 ; Remove the hex digit bit.
; Allow both upper and lower case apha characters.
if type == 771 || type == 770 then Text = "is ASCII"
else text = "is not all ASCII"

Message(str, text)


Title: Re: IsAscii( )
Post by: mueer01 on November 24, 2025, 11:20:14 PM
many thanks for the answers.
Title: Re: IsAscii( )
Post by: td on November 26, 2025, 07:35:25 AM
The second line of the script I posted should be

Type = StrTypeInfo(str, 1) & (~128) ; Remove the hex digit bit.