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

Discussion in 'Javascript' started by Billy Smith, Apr 23, 2006.

  1. Billy Smith

    Billy Smith Guest

    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
    Billy Smith, Apr 23, 2006
    #1
    1. Advertising

  2. "Billy Smith" <> writes:

    > 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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Apr 23, 2006
    #2
    1. Advertising

  3. Billy Smith

    Billy Smith Guest

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


    "Lasse Reichstein Nielsen" <> wrote in message
    news:...
    > "Billy Smith" <> writes:
    >
    >> 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
    > --
    > Lasse Reichstein Nielsen -
    > DHTML Death Colors:
    > <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    > 'Faith without judgement merely degrades the spirit divine.'
    Billy Smith, Apr 23, 2006
    #3
  4. Lasse Reichstein Nielsen wrote:

    > "Billy Smith" <> writes:
    >> 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.


    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
    Thomas 'PointedEars' Lahn, Apr 23, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Benjamin Bittner

    IISFAQ WSH Script in/to DotNet

    Benjamin Bittner, Jun 28, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    863
    M. Zeeshan Mustafa
    Jun 28, 2004
  2. David Josty

    wsh and Python

    David Josty, Apr 9, 2005, in forum: Python
    Replies:
    1
    Views:
    453
    Roger Upole
    Apr 10, 2005
  3. scad
    Replies:
    4
    Views:
    949
    James Kanze
    May 28, 2009
  4. Sojwal

    Can't execute WSH script from bat file

    Sojwal, Aug 13, 2004, in forum: ASP General
    Replies:
    0
    Views:
    100
    Sojwal
    Aug 13, 2004
  5. Adelson Anton

    Batch File Manipulation With WSH

    Adelson Anton, Apr 24, 2004, in forum: Javascript
    Replies:
    0
    Views:
    71
    Adelson Anton
    Apr 24, 2004
Loading...

Share This Page