How do I write binary data to a file with javascript & WSH?

B

Billy Smith

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 it refused to write any of the extended ASCII
characters but it accepts some and not others. For example
it will write characters with codes 157, 255, and 188 but
fails on 156 and 128, both of which are printable characters.
It works with ordinary characters (codes 0-127) just fine.

1) Why is it letting me write some extended ASCII characters and
not others?

2) It's beginning to look to me like javascript and WSH don't
provide a way to write binary data. Am I right or is there
a way to write binary data to a file using these tools?

Thanks,
Billy
 
L

Lasse Reichstein Nielsen

Billy Smith said:
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 it refused to write any of the extended ASCII
characters but it accepts some and not others. For example
it will write characters with codes 157, 255, and 188 but
fails on 156 and 128, both of which are printable characters.

Neither 128 nor 156 are ASCII characters, nor are they defined in
ISO-8859-1. In Unicode, those code points are control characters
(128 is apparently an undefined control character, and 156 is
"single character introducer").

Apparently textual output of these code points fails.

You should also be aware that code point 128 is not represented
as one byte using UTF-8 encoding (or any other standardized
encoding of Unicode), so even if it can be written, it won't give
you what you want.
It works with ordinary characters (codes 0-127) just fine.

As it should, these are Unicode characters as well.
2) It's beginning to look to me like javascript and WSH don't
provide a way to write binary data. Am I right or is there
a way to write binary data to a file using these tools?

There is nothing in Javascript itself. I don't know the capabilities
of WSH. But a Google on "wsh write binary file" seems useful.

/L
 
B

Billy Smith

OK, I guess I'm not thinking in terms of the correct encoding. I'll
go learn about the encoding javascript uses. Thanks.
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Neither 128 nor 156 are ASCII characters, nor are they defined in
ISO-8859-1. In Unicode, those code points are control characters
(128 is apparently an undefined control character, and 156 is
"single character introducer").

Apparently textual output of these code points fails.

You should also be aware that code point 128 is not represented
as one byte using UTF-8 encoding (or any other standardized
encoding of Unicode), so even if it can be written, it won't give
you what you want.

I agree with all what you said, but not with the wording. A *code point*
is a coordinate, an _integer number_ (usually given in hexadecimal). It
is _not_ a character. It /specifies/ the _position of a character_,
therefore /identifies/ that character, in a character set (here: the
Unicode character set). The character requires encoding to be represented,
which is performed according to its code point.

So it would be correct to say that characters at certain code points of a
character set are control characters, and that a character at a certain
code point is not encoded in a certain way using a certain encoding
(here: UTF-8).

<URL:http://en.wikipedia.org/wiki/Code_point>


PointedEars
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top