Binary I/O in Javascript

J

Julian Turner

VK said:
... not for criticism of any kind, I'd just like so save some of my
time/

I will get the files together and post in the next day or so. Feel
free to criticise.

Julian
 
J

Julian Turner

Jasen said:
Unicode was 20 bits wide last time I looked

Sorry, thats a fair point.

I should have been clearer. For the purposes of using a Javascript
string for byte processing purposes, the ECMA 262 specification
states:-

"A string value ... is a finite ordered sequence of zero or more 16-bit
unsigned integers.

NOTE

Although each value usually represents a 16bit unit of UTF-16 text, the
language does not place any restrictions or requirements on the values
except that they be 16 bit unsigned integers.

Julian
 
T

Thomas 'PointedEars' Lahn

Jasen said:
Patient said:
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

Unicode was 20 bits wide last time I looked

That statement is false per se since Unicode is not a data format but
a character set. "Unicode (4.1.0) characters may be encoded at any code
point from U+0000 to U+10FFFF"[1] in binary digits using a UTF (Unicode
Transformation Format).

However, the maximum code point does not define the maximum number of bits
used to display a character. There are several variants of UTFs available:
UTF-7, UTF-8, UTF-16 and UTF-32, each indicating the length in bits one
code unit may have. Meaning that a character with a code point requiring
20 bits (such as U+FEDCB) can be represented by 3 units of UTF-7 (21 bits
total), 3 units of UTF-8 (24 bits total), 2 units of UTF-16 (32 bits total)
or 1 unit of UTF-32 (32 bits total). (CMIIW)

ECMAScript implementations, including JavaScript 1.3+ and JScript, use
UTF-16 to store string values, which means that every character requires
at least 16 bits to be stored, but not that every character requries only
16 bits to be stored. It depends on its code point: if beyond the 63K
border, 32 bits will be used.

[1] <http://www.unicode.org/faq/>


PointedEars
 
J

Julian Turner

Julian said:
I will get the files together and post in the next day or so. Feel
free to criticise.

Here are the functions I have come up with. Remember these are IE
only.

Only managed binary save through VB (which is effectively off-topic).

Takes a Javascript String holding a set of bytes as an input.

Function VBBinarySave(SaveString,FileName)

Dim Index
Dim ByteString
Dim Char

For Index = 1 to Len(SaveString)
ByteString = ByteString & ChrB(AscB(Mid(SaveString,Index,1)))
Next

Set FS = CreateObject("Scripting.FileSystemObject")
Set File = FS.CreateTextFile(FileName, True)
For Index = 1 to LenB(ByteString)
Char=Chr(AscB(MidB(ByteString,Index,1)))
File.Write Char
Next

File.Close

End Function

And Javascript load (perhaps as we are using VB, could be in VB as
well, but then that really would be off-topic):-

Outputs an array of Numbers, each holding 1 byte.

function BinaryLoad(sFileName)
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.GetFile(sFileName);
if (!f){return false;}
var ts = f.OpenAsTextStream(1,0);
var s = ts.ReadAll();

var a=[];
var n;
var c;
var
CON={8364:128,129:129,8218:130,402:131,8222:132,8230:133,8224:134,8225:135,710:136,8240:137,352:138,8249:139,338:140,141:141,381:142,143:143,144:144,8216:145,8217:146,8220:147,8221:148,8226:149,8211:150,8212:151,732:152,8482:153,353:154,8250:155,339:156,157:157,382:158,376:159};

var i=0;
var l=s.length;

while(i<l)
{
n=s.charCodeAt(i);
c=(n in CON)? CON[n]:n;
a[a.length]=c;
i++;
}

return a;
};

Julian
 
W

weston

Patient said:
Has anyone written code that successfully manipulates binary file data
using Javascript?

This guy:

http://www.elf.org/pnglets/

apparently did back in 1999.

It doesn't seem to work in IE, however, and it appears to be because IE
doesn't like the javascript: URL method of feeding the browser image
data. I'm a bit surprised by this. I'd thought that since the
Wolfenstein 5k guy had been able to get IE to swallow XBM information
this way, it should work fine for gifs or PNGs too.

http://www.wolf5k.com/faq.html#2

But apparently not:

http://weston.canncentral.org/web_lab/pnglets/test.html

Does anyone know why? I know that XBM is a character based format, and
so I suspect that it has something to do with some kind of
string/binary dichotomy -- maybe IE uses a funny encoding, or takes the
strings literally?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top