Patient Guy wrote:
Has anyone written code that successfully manipulates binary file data
using Javascript?
It might---and in the case of doing I/O, will---make use of browser-
specific functions (ActiveX/COM with Internet Explorer, XPCOM/XPConnect
with Mozilla/Firefox).
[snip]
You can certainly manipulate binary data in a rough fashion, using a
Javascript String. A Javascript String format is in Unicode pairs I
believe. I.e. each character can hold a value from 0000 to FFFF, so
the binary stream could be represented either as a sequence where the
higher pair is 00, and the byte is the lower, i.e. 0000, 0001, 0002
.... 00FF, or each pair of bytes is encoded in to the Unicode pairs,
e.g. 0001, 0203, 0304, ... FEFF. The latter case is problematic for
odd numbered streams.
The trick however is getting the binary codes from files into and out
of the String object in the first place, whether at all, or
efficiently.
I have been experimenting myself, and so far Javascript alone does not
seem to be enough.
INTERNET EXPLORER
There are two ActiveX objects to consider:-
(a) Scripting.FileS ystemObject
Through Javascript, it seems to be impossible to use this to save
strings containing a binary stream. If encoding is set to ASCII, an
error will be thrown for char codes in the range 80 - A0 which you try
to write to a file. If encoding is Unicode, then the resulting file is
prefixed with FF FE, and it only works for even numbered streams.
You can use it to load binary into Strings, but you need again to
watch for characters 80-A0, as they are converted during the load
process to some other unicode characters. This is consistently done,
so once you know the conversion, i.e. 80->2030 etc, you can easily
reverse it.
However, oddly, in VBScript, with encoding set to ASCII you can
actually write binary using Scripting.FileS ystemObject. I think this
is because VBScript has other data types which are more acceptable to
the ActiveX object.
So you can adopt a mixture of VBScript and Javascript to get a kind of
binary i/o.
(a) ADODB.Stream
This gives full binary file system access. I think however it is
disabled by default on later versions of windows. In any event, it has
one limitation again in Javascript if you want to write binary data
from a String to the stream: the WriteText method again fails with
characters in the range 80-A0.
(c) XML dataType
A third area to explore is setting an XML node data type to bin.hex or
bin.base64. You could convert a String to base64 or hex, and set the
nodeTypedValue with that encoded string. This is more useful for
HTTPRequests, but conversion to base64 and hex is very slow. When you
subsequently bet the nodeTypedValue it will return a Variant byte
buffer containing the binary. Useless in Javascript, but might be
useable with ADODB.Stream or VBScript.
MOZILLA
I am not as familiar with the XPCOM components - see
www.xulplanet.com,
but they may have some scriptable input and output streams. Again
don't know how they would interact with the Javascript String type.
Julian