Is there anything better than escape()

Discussion in 'Javascript' started by simplicity, Dec 17, 2007.

  1. simplicity

    simplicity Guest

    I need to upload the local file to the server site in the environment
    where there is no webserver running there, hence no POST method is
    available. All I have at the receiving end is the SOAP server which
    means that I must send the file as the SOAP message containing textual
    representation of bytes, for example:

    fileContent = "a b<LF>" must be sent as <data>613262320A</data> where
    61 is a hex value of 'a', 32 of SPACE, 62 is 'b', 0A is LF etc

    I tried replace() but I am running into a problem of premature end of
    array - replace() deals with String types and will do the conversion
    to something like "a%32b%0A" (which can be further processed easily to
    convert all typable characters by their hex values while stripping %
    from non-typable ones) but it will quit on the first accurence of 00
    (null) byte and, no need to mention, the binary file will be full of
    those.

    I can step through the fileContent array one byte at a time and
    traverse "special bytes" including null (byte value 0x00) up to the
    length of the file I read but I do not know how to determine what
    exactly the non-typable characters are, that is I can do mapping

    if (content == 'a') concatenate "61" to the SOAP message

    but I don't know how I can deal with something like <BEL> character
    (byte value 0x07) or any byte value above 0x7F.

    Is there a way out of this?
    simplicity, Dec 17, 2007
    #1
    1. Advertising

  2. simplicity wrote:
    > fileContent = "a b<LF>" must be sent as <data>613262320A</data> where
    > 61 is a hex value of 'a', 32 of SPACE, 62 is 'b', 0A is LF etc
    >
    > I tried replace() but I am running into a problem of premature end of
    > array - replace() deals with String types and will do the conversion
    > to something like "a%32b%0A"


    You mean escape(), not String.prototype.replace(). Both methods operate on
    string values.

    You are looking for String.prototype.replace(), not escape():

    fileContent = '<data>'
    + fileContent.replace(
    /[\s\S]/g,
    function(m)
    {
    return m.charCodeAt(0).toString(16).toUpperCase();
    })
    + '</data>';

    Be sure to escape the `</' as `<\/' when in CDATA content.


    HTH

    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
    Thomas 'PointedEars' Lahn, Dec 18, 2007
    #2
    1. Advertising

  3. simplicity

    simplicity Guest

    On Dec 17, 2:56 pm, simplicity <> wrote:
    > I need to upload the local file to the server site in the environment
    > where there is no webserver running there, hence no POST method is
    > available. All I have at the receiving end is the SOAP server which
    > means that I must send the file as the SOAP message containing textual
    > representation of bytes, for example:
    >
    > fileContent = "a b<LF>" must be sent as <data>613262320A</data> where
    > 61 is a hex value of 'a', 32 of SPACE, 62 is 'b', 0A is LF etc
    >
    > I tried replace() but I am running into a problem of premature end of
    > array - replace() deals with String types and will do the conversion
    > to something like "a%32b%0A" (which can be further processed easily to
    > convert all typable characters by their hex values while stripping %
    > from non-typable ones) but it will quit on the first accurence of 00
    > (null) byte and, no need to mention, the binary file will be full of
    > those.
    >
    > I can step through the fileContent array one byte at a time and
    > traverse "special bytes" including null (byte value 0x00) up to the
    > length of the file I read but I do not know how to determine what
    > exactly the non-typable characters are, that is I can do mapping
    >
    > if (content == 'a') concatenate "61" to the SOAP message
    >
    > but I don't know how I can deal with something like <BEL> character
    > (byte value 0x07) or any byte value above 0x7F.
    >
    > Is there a way out of this?


    Thanks Marc and Thomas

    Both code samples work like a charm until... well it is probably my
    fault that my description of the problem contained errors and was far
    from accurate.

    First you both caught the erroneous info correctly: of course I want
    to convert my sample bytes "a b<LF>" into <data>6120620A</data>, then
    I experimented with escape() not replace().

    Second, a big mistake I made was when I claimed that "I can (...)
    traverse "special bytes" including null (byte value 0x00)". The truth
    is: all of them except null. I guess I missed that fact because I
    buried that null (byte value 00) close to the end of the file and lost
    the count :-:)-(. The reality is that when I put 00 as a first byte, I
    read the length of the file as it truly is (in my case study 32 bytes)
    but when I read the file into fileContent and step through fileContent
    it interprets fileContent[0] = 0 as end of string and every next
    fileContent is also returning 0.

    As there are only two data types in javascript, strings and numbers,
    is there a trick which would allow me to read the file into the array
    of numbers which would remove the "special meaning" from all elements
    of value 0"? I know what size the file is before I read it, so
    allocating the array of correct size will not be a problem.

    Stella
    simplicity, Dec 19, 2007
    #3
  4. simplicity wrote:
    > [...] The reality is that when I put 00 as a first byte, I read the
    > length of the file as it truly is (in my case study 32 bytes) but when I
    > read the file into fileContent and step through fileContent it interprets
    > fileContent[0] = 0 as end of string and every next fileContent is also
    > returning 0.


    See below.

    > As there are only two data types in javascript, strings and numbers,


    You are mistaken. Read any reference on the language to find the contrary
    confirmed.

    > is there a trick which would allow me to read the file into the array of
    > numbers which would remove the "special meaning" from all elements of
    > value 0"? I know what size the file is before I read it, so allocating
    > the array of correct size will not be a problem.


    The NUL byte (0x00) denotes the end of a character string of variable length
    in many formats (but not in ECMAScript strings). Other than that, I don't
    know what your file reading problem is, but there is surely no ECMAScript
    data type problem: "\x00".charCodeAt(0).toString(16) returns "0".

    Of course, when you have to deal with characters at code points below
    U+0010, you will have to pad the returned value with "0" in order to have a
    proper byte code. And you will have to exclude characters with code points
    above U+00FF, unless your XML parser can deal with Unicode code points:

    fileContent = '<data>'
    + fileContent.replace(
    /[\s\S]/g,
    function(m)
    {
    var cp = m.charCodeAt(0);

    if (cp > 0xFF) return "";

    var s = cp.toString(16).toUpperCase();

    return (s.length < 1) ? "0" + s : s;
    })
    + '</data>';


    Reader's time and bandwidth are precious. Please trim your quotes to
    the minimum required to retain the context of your reply. See also
    http://jibbering.com/faq/ pp.


    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
    Thomas 'PointedEars' Lahn, Dec 19, 2007
    #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. Shawn Repphan

    Anything better than Panel control?

    Shawn Repphan, Nov 1, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    373
    Mythran
    Nov 1, 2005
  2. Alessandro Bottoni

    Is there anything "better" than impalib/poplib?

    Alessandro Bottoni, Sep 7, 2005, in forum: Python
    Replies:
    1
    Views:
    253
    Thomas Guettler
    Sep 7, 2005
  3. Peter Bencsik
    Replies:
    2
    Views:
    826
  4. Roy Smith

    Anything better than shutil?

    Roy Smith, Nov 14, 2009, in forum: Python
    Replies:
    2
    Views:
    696
    Steven D'Aprano
    Nov 15, 2009
  5. Paganoni
    Replies:
    9
    Views:
    118
    Gregory Brown
    Apr 2, 2009
Loading...

Share This Page