Reputation: 2681
I want to keep user from entering character > ASCII 127 in one of the password field in installer.
I googled but didn't find any direct way for this, currently i'm using:
CHAR ch;
STRING ASCII;
NUMBER nASCII;
for nCount = 0 to StrLength(sPassword)
ch = sPassword[nCount];
sprintf(ASCII,"%d",ch);
StrToNum(nASCII,ASCII);
if ( nASCII > 127 )
MessageBox("Invalid Character in Password",INFORMATION);
endif;
endfor;
Is there any better way to get ASCII code from a string?
Upvotes: 0
Views: 1253
Reputation: 15905
First off, I would avoid the conversion to and from string; just compare sPassword[nCount]
with 127
directly; InstallScript stores wide chars (16-bit numbers).
As an alternative approach, you might try calling WideCharToMultiByte with the US-ASCII code page (20127). I'm not that strong with InstallScript and am coding without the compiler, so you might have to fix an error or two, but here's the rough idea:
#define CP_US_ASCII 20127
extern prototype NUMBER Kernel32.WideCharToMultiByte(NUMBER, NUMBER, WSTRING, NUMBER, STRING, NUMBER, STRING, BYREF BOOL);
function BOOL IsSafeAscii(STRING szCheck)
STRING szOut;
BOOL bReplaced;
begin
WideCharToMultiBute(CP_US_ASCII, // only supports characters 0-127
WC_NO_BEST_FIT_CHARS, // or maybe 0; disallow turning accented to plain
szCheck, // test string
StrLength(szCheck), // length of test string
szOut, // return buffer
StrLength(szOut), // length of return buffer
"?", // replacement for unsupported characters
bReplaced); // whether replacement was used
return !bReplaced;
end;
Upvotes: 1