faqts : Computers : Programming : Languages : Perl

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 3 people (33%) answered Yes
Recently 1 of 3 people (33%) answered Yes

Entry

How do I distinguish between printable strings andpacked binary data stored as strings?

Dec 1st, 2004 09:09
Nat Tuck, flurpy flurp,


They're the same data type - there's no good way to tell them apart.
Optimally you should write your program so it knows what's going on and
gets data in a specific format when it expects data in that format.
If you really need to tell the difference between random binary data and
readable strings you'd be able to get a flakey result by checking to see
if any of the characters in the string have ascii values above 127 - if
so it's probably binary data. That has two problems: Some legitimate
characters such as accented characters may have values above 127 and
It's possible (1 in 2^#bytes in unbiased data) that you luck into binary
data with no values above 127.