473,890 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary I/O in Javascript



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).

I am writing client-side code that will generate binary data for producing
a GIF file through an OBJECT element. (The GIF is an image of a line and
points on a two-axis plot.)

In order to do some tests to see how the OBJECT element performs, I am
trying to read in binary file data (using XPConnect functions in Firefox).

The problem is that plain Javascript and these browser-specific functions
will not marry with one another (for instance, they cannot be manipulated
as strings, and the read() functions of these things return strings as
well). This is from what little I know. Dated posted in various
newsgroups on the subject get comments saying that if those creating file
I/O libraries in client-side code for text file I/O might as well make it
possible for manipulating binary data in I/O, since the point of no return
is authorizing any access to the file system, and not whether it is text or
binary access.

If I don't get an answer here, I will take this to the JScript and XPCOM
groups, where I can hope for more from the latter than the former. If it
can't be done in MSIE though, I suppose I should forget it (and try to
write a plugin of some kind instead). Unless I hear that MSIE browsers are
being used by less than 100,000 people around the world.
Nov 23 '05
26 4343

VK wrote:
... 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

Nov 23 '05 #21
On 2005-11-14, Julian Turner <ju****@baconbu tty.com> wrote:
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


Unicode was 20 bits wide last time I looked

--

Bye.
Jasen
Nov 23 '05 #22

Jasen Betts wrote:
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

Nov 23 '05 #23
Jasen Betts wrote:
On 2005-11-14, Julian Turner <ju****@baconbu tty.com> wrote:
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


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
Nov 23 '05 #24

Julian Turner wrote:
VK wrote:
... 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.


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(Sa veString,FileNa me)

Dim Index
Dim ByteString
Dim Char

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

Set FS = CreateObject("S cripting.FileSy stemObject")
Set File = FS.CreateTextFi le(FileName, True)
For Index = 1 to LenB(ByteString )
Char=Chr(AscB(M idB(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(sFil eName)
{
var fso = new ActiveXObject(" Scripting.FileS ystemObject");
var f = fso.GetFile(sFi leName);
if (!f){return false;}
var ts = f.OpenAsTextStr eam(1,0);
var s = ts.ReadAll();

var a=[];
var n;
var c;
var
CON={8364:128,1 29:129,8218:130 ,402:131,8222:1 32,8230:133,822 4:134,8225:135, 710:136,8240:13 7,352:138,8249: 139,338:140,141 :141,381:142,14 3:143,144:144,8 216:145,8217:14 6,8220:147,8221 :148,8226:149,8 211:150,8212:15 1,732:152,8482: 153,353:154,825 0:155,339:156,1 57: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

Nov 23 '05 #25
Patient Guy wrote:
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?

Dec 10 '05 #26
Followup... Critchelow of that elf.org site seems to have been thinking
about IE's behavior with regard to inline data back then, too:

http://www.elf.org/essay/inline-image.html

and a few people seem to have been on the trail in the meanwhile:

http://dean.edwards.name/weblog/2005/06/base64-ie/
http://www.kryogenix.org/days/2003/10/18/embedding

Dec 10 '05 #27

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
5679
by: joealey2003 | last post by:
Hi all... I need to load an image, access it's content and render it. So, tried: <script type="text/javascript"> xmlHttp = new XMLHttpRequest();
9
6560
by: J.Sperlhofer | last post by:
Good morning, Javascript-Professionals. I'm looking for an possibility to show a (calculated) 64bit-Number without exponential notation. I don't want to see exponational notation within my binary numbers. To demonstrate my problem, try this code: ---- var binNumber = Math.pow(2,61);
4
4293
by: CSharpener | last post by:
This should be *so* easy! How do I convert a Byte or int to a binary string representation in C# In JavaScript, it goes like this for an int: javascript:(123).toString(2 or javascript:(0xFE).toString(2 No problem. So, how do I do the same in C#? What simple thing am I missing
4
8333
by: dirtside | last post by:
I've search far and wide for an answer, so forgive me if this is a stupid question. (Well, it probably is.) Consider the following JavaScript: var foo = new Image(); foo.src = "http://some.server.com/some_filename.gif"; Now, the browser will retrieve 'some_filename.gif' from some.server.com and put it into the object foo. What I want to do is then subsequently
9
6309
by: GiantCranesInDublin | last post by:
Hi, I am looking for the best performing solution for modifying and iterating an object graph in JavaScript. I have outlined below a simplified example of the object model and examples of how I will be using this graph. Object model:
15
9827
by: mleaver | last post by:
I want to open a second window and display a binary image that is returned from a java program via XMLRPC. The data returned is a binary encoded base64 png file. If I write the data out to a file on my server, I can display it using the following javascript: var windowHandle = window.open('about:blank','windowName','width=250,height=250'); windowHandle.document.write('<img name="myImage" src="images/test.png">');
7
2869
by: robert.differentone | last post by:
Hi, Does anybody know any javascript which converts images into binary data. Thanks in advance, R.
3
8550
by: Billy Smith | last post by:
I'm trying to write a little utility that will write some binary data to a file via a javascript and Windows Script Host under Windows XP. The only way to do this that I can find is to convert the binary data to text via String.fromCharCode() function and then write to the file with TextStream.Write(). But that function gives an "invalid parameter" error message when I try to write some ASCII codes to the file. I could understand if...
0
28099
Frinavale
by: Frinavale | last post by:
Convert a Hex number into a decimal number and a decimal number to Hex number This is a very simple script that converts decimal numbers into hex values and hex values into decimal numbers. The example following this one demonstrates how to convert decimal numbers into binary values. A hex number is a string that contains numbers between 0-9 and characters A-F; where A = 10, B = 11, ..., F = 15. (For more information please look up...
0
10801
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9616
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8007
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7159
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6034
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4662
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4258
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.