By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,256 Members | 1,663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,256 IT Pros & Developers. It's quick & easy.

How do I turn every chactor into %xx style?

P: n/a
ok
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
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ok wrote on 24 sep 2004 in comp.lang.javascript:
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.
<script type='text/javascript'>

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

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

</script>
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.


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)

Jul 23 '05 #2

P: n/a
ok wrote:
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.


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

http://lab.artlung.com/scripting/urlencode/

Checkout encodeURI too.

Cheers, Fred.
Jul 23 '05 #3

P: n/a
"Evertjan." <ex**************@interxnl.net> wrote in
news:Xn********************@194.109.133.29:
ok wrote on 24 sep 2004 in comp.lang.javascript:
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.
<script type='text/javascript'>

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


should be in hex, doubt that it is... Also, it should be *two* hex
digits afaik, ie. %a%b%c is wrong.
};
return tt;
}

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

</script>
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.


You could, but html cannot read that.


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
Jul 23 '05 #4

P: n/a
John Bokma wrote on 24 sep 2004 in comp.lang.javascript:
"Evertjan." <ex**************@interxnl.net> wrote in
news:Xn********************@194.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.

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

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

<script type='text/javascript'>

var r = '0123456789ABCDEF'

function toPerc(t){
var tt = '';
for (var i=0;i<t.length;i++){
x = t.charCodeAt(i)
tt += "%"+r.substr(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)

Jul 23 '05 #5

P: n/a
"Evertjan." <ex**************@interxnl.net> wrote in
news:Xn*******************@194.109.133.29:
John Bokma wrote on 24 sep 2004 in comp.lang.javascript:
"Evertjan." <ex**************@interxnl.net> wrote in
news:Xn********************@194.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.
You are right, if the html standard expects hex. And it does. ;-)


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


:-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
Jul 23 '05 #6

P: n/a
"Evertjan." wrote:
John Bokma wrote on 24 sep 2004 in comp.lang.javascript:
"Evertjan." <ex**************@interxnl.net> wrote in
news:Xn********************@194.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.


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

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

<script type='text/javascript'>

var r = '0123456789ABCDEF'

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

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

</script>


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

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

I wanted to use:

tt += '%' + ('0' +
t.charCodeAt(i).toString(16).toUpperCase()).substr (-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 <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.