views:

853

answers:

3

Is it possible to detect binary data in JavaScript?

I'd like to be able to detect binary data and convert it to hex for easier readability/debugging.


After more investigation I've realized that detecting binary data is not the right question, because binary data can contain regular characters, and non-printable characters.

Outis's question and answer (/[\x00-\x1F]/) is really the best we can do in an attempt to detect binary characters.

Note: You must remove line feeds and possibly other characters from your ascii string sequence for the check to actually work.

+2  A: 

If by "binary", you mean "contains non-printable characters", try:

data.test(/[\x00-\x1F]/)

If you know the string is either ASCII or binary, use:

data.test(/[\x00-\x1F\x80-\xFF]/)
outis
A: 

I think you may find your answer in this article It's pretty heavy-duty though

DroidIn.net
A: 

If you use json2.js or http://www.openjsan.org/doc/k/ke/kevinj/Data/Dump/0.01/lib/Data/Dump.html you don't have to worry about detecting non-printable characters

anonymous