How do I turn every chactor into %xx style?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ok

    How do I turn every chactor into %xx style?

    I tried escape, but it is only good for the special ones like : / ' space
    etc. I want to turn every charactor into %xx then document.write them.

    I wonder if i can double % them so I can turn @ into %40, then %%xx%yy xx is
    for 4 and yy is for 0.

    Thanks


  • Evertjan.

    #2
    Re: How do I turn every chactor into %xx style?

    ok wrote on 24 sep 2004 in comp.lang.javas cript:[color=blue]
    > I tried escape, but it is only good for the special ones like : / '
    > space etc. I want to turn every charactor into %xx then document.write
    > them.[/color]

    <script type='text/javascript'>

    function toPerc(t){
    var tt = '';
    for (var i=0;i<t.length; i++){
    tt += "%"+t.charCodeA t(i);
    };
    return tt;
    }

    document.write( toPerc('Blah blah'));

    </script>
    [color=blue]
    > I wonder if i can double % them so I can turn @ into %40, then %%xx%yy
    > xx is for 4 and yy is for 0.[/color]

    You could, but html cannot read that.

    did you try?


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress,
    but let us keep the discussions in the newsgroup)

    Comment

    • Fred Oz

      #3
      Re: How do I turn every chactor into %xx style?

      ok wrote:[color=blue]
      > I tried escape, but it is only good for the special ones like : / ' space
      > etc. I want to turn every charactor into %xx then document.write them.
      >
      > I wonder if i can double % them so I can turn @ into %40, then %%xx%yy xx is
      > for 4 and yy is for 0.
      >[/color]

      Not really sure what you are trying to do here - encode is dpreciated
      in favour of encodeURI. If you are after a way of obfuscating your
      code, Google "javascript encode"

      Have a look at the link below, it includes a script to convert all
      characters into their JavaScript encoding. Note that the encoding for
      normal numbers and alphas is themselves,
      i.e. the encoding for "a" is "a".



      Checkout encodeURI too.

      Cheers, Fred.

      Comment

      • John Bokma

        #4
        Re: How do I turn every chactor into %xx style?

        "Evertjan." <exjxw.hannivoo rt@interxnl.net > wrote in
        news:Xns956E5B3 9CECBFeejj99@19 4.109.133.29:
        [color=blue]
        > ok wrote on 24 sep 2004 in comp.lang.javas cript:[color=green]
        >> I tried escape, but it is only good for the special ones like : / '
        >> space etc. I want to turn every charactor into %xx then[/color][/color]
        document.write[color=blue][color=green]
        >> them.[/color]
        >
        > <script type='text/javascript'>
        >
        > function toPerc(t){
        > var tt = '';
        > for (var i=0;i<t.length; i++){
        > tt += "%"+t.charCodeA t(i);[/color]

        should be in hex, doubt that it is... Also, it should be *two* hex
        digits afaik, ie. %a%b%c is wrong.
        [color=blue]
        > };
        > return tt;
        > }
        >
        > document.write( toPerc('Blah blah'));
        >
        > </script>
        >[color=green]
        >> I wonder if i can double % them so I can turn @ into %40, then %%xx%[/color][/color]
        yy[color=blue][color=green]
        >> xx is for 4 and yy is for 0.[/color]
        >
        > You could, but html cannot read that.[/color]

        Has nothing to do with HTML, but what your browser can do. And it can't.

        --
        John MexIT: http://johnbokma.com/mexit/
        personal page: http://johnbokma.com/
        Experienced programmer available: http://castleamber.com/
        Happy Customers: http://castleamber.com/testimonials.html

        Comment

        • Evertjan.

          #5
          Re: How do I turn every chactor into %xx style?

          John Bokma wrote on 24 sep 2004 in comp.lang.javas cript:
          [color=blue]
          > "Evertjan." <exjxw.hannivoo rt@interxnl.net > wrote in
          > news:Xns956E5B3 9CECBFeejj99@19 4.109.133.29:
          >
          > should be in hex, doubt that it is... Also, it should be *two* hex
          > digits afaik, ie. %a%b%c is wrong.[/color]


          You are right, if the html standard expects hex. And it does. ;-)

          =============== ==============

          <script type='text/javascript'>

          var r = '0123456789ABCD EF'

          function toPerc(t){
          var tt = '';
          for (var i=0;i<t.length; i++){
          x = t.charCodeAt(i)
          tt += "%"+r.subst r(x/16,1)+r.substr( x%16,1);
          };
          return tt;
          }

          document.write( toPerc('123Blah blah'));

          </script>



          --
          Evertjan.
          The Netherlands.
          (Please change the x'es to dots in my emailaddress,
          but let us keep the discussions in the newsgroup)

          Comment

          • John Bokma

            #6
            Re: How do I turn every chactor into %xx style?

            "Evertjan." <exjxw.hannivoo rt@interxnl.net > wrote in
            news:Xns956E9DC B75F4eejj99@194 .109.133.29:
            [color=blue]
            > John Bokma wrote on 24 sep 2004 in comp.lang.javas cript:
            >[color=green]
            >> "Evertjan." <exjxw.hannivoo rt@interxnl.net > wrote in
            >> news:Xns956E5B3 9CECBFeejj99@19 4.109.133.29:
            >>
            >> should be in hex, doubt that it is... Also, it should be *two* hex
            >> digits afaik, ie. %a%b%c is wrong.[/color]
            >
            > You are right, if the html standard expects hex. And it does. ;-)[/color]

            It has nothing to do with HTML, but with URI encoding.
            [color=blue]
            > tt += "%"+r.subst r(x/16,1)+r.substr( x%16,1);[/color]

            :-D.

            --
            John MexIT: http://johnbokma.com/mexit/
            personal page: http://johnbokma.com/
            Experienced programmer available: http://castleamber.com/
            Happy Customers: http://castleamber.com/testimonials.html

            Comment

            • Grant Wagner

              #7
              Re: How do I turn every chactor into %xx style?

              "Evertjan." wrote:
              [color=blue]
              > John Bokma wrote on 24 sep 2004 in comp.lang.javas cript:
              >[color=green]
              > > "Evertjan." <exjxw.hannivoo rt@interxnl.net > wrote in
              > > news:Xns956E5B3 9CECBFeejj99@19 4.109.133.29:
              > >
              > > should be in hex, doubt that it is... Also, it should be *two* hex
              > > digits afaik, ie. %a%b%c is wrong.[/color]
              >
              > You are right, if the html standard expects hex. And it does. ;-)
              >
              > =============== ==============
              >
              > <script type='text/javascript'>
              >
              > var r = '0123456789ABCD EF'
              >
              > function toPerc(t){
              > var tt = '';
              > for (var i=0;i<t.length; i++){
              > x = t.charCodeAt(i)
              > tt += "%"+r.subst r(x/16,1)+r.substr( x%16,1);
              > };
              > return tt;
              > }
              >
              > document.write( toPerc('123Blah blah'));
              >
              > </script>[/color]

              function toPerc(t){
              var tt = '', hex;
              for (var i = 0; i < t.length; ++i) {
              tt += '%' + (hex = ('0' +
              t.charCodeAt(i) .toString(16).t oUpperCase())). substring(hex.l ength - 2);
              }
              return tt;
              }
              document.write( toPerc('123\t\0 Blah blah'));

              Avoids a "magic" string containing the available hex digits.

              I wanted to use:

              tt += '%' + ('0' +
              t.charCodeAt(i) .toString(16).t oUpperCase()).s ubstr(-2);

              which would have avoided the awkward assignment to "hex", but IE doesn't
              respect the substr() contract as far as negative first parameters are
              concerned.

              --
              Grant Wagner <gwagner@agrico reunited.com>
              comp.lang.javas cript FAQ - http://jibbering.com/faq

              Comment

              Working...