Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Verification of only valid ASCII Characters in Input Buffer 1

Status
Not open for further replies.

jbs

Programmer
Feb 19, 2000
121
0
0
US
Given a null-terminated char array what is the most efficient method to validate that each byte contains a valid ASCII character (i.e. A-Z, 0-9, CR, LF, ..).

Curious as to opinions here...

thanks,
 
jbs,

Not sure how efficient but you could loop for all characters in the array and check for

__isascii

which returns a true for ascii char and false if not.

Hope this helps

 
Actually that sounds great. I hadn't heard of __isascii before and will try it out. Seems like that will require less lines of code than what I was thinking of....thanks for the time./j
 
isascii() returns true for all characters between 0x00 & 0x7F (which includes a lot of non-printable characters).
You might be better off using isprint() which only returns true for chars 0x20 to 0x7E.
 
cpjust,

jbs mentioned that he wanted to see CR and LF so that is why i suggested __isascii

 
Oops, missed that part. But it still depends on what kind of data he's trying to validate. If it's text data, then isascii() will let a lot of junk through. In that case you'd need to use both isprint() and isspace().
Code:
if ( isprint( c ) || isspace( c ) )
{
   ...
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top