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

What is the JavaScript equivalents for Chr() and Asc() (from BASIC)

P: n/a
Hi Gang,

I would like to use a caesar cipher on the mailto: links in my website
to prevent crawlers from farming the e-mails off my site. Can someone
tell me the equivalents to Chr() and Asc() from Visual Basic, but for
JavaScript? Chr(n) returns the character based on the numeric ASCII
code. For example
Chr(34) --> "
Asc(") --> 34

Thanks,
Christian Blackburn

Jun 14 '06 #1
Share this Question
Share on Google+
5 Replies

P: n/a

Christian Blackburn wrote:

[snip]
Chr(34) --> " [/snip]

String.fromCharCode
e.g.

var sChar=String.fromCharCode(34);

[snip] Asc(") --> 34

[/snip]

charCodeAt(nIndex) returns the char code for the character at a given
index position within a string (zero based index).

Thus if you want the code for a single character:-

var nCharCode="\"".charCodeAt(0);

See
<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:String:charC odeAt>

Regards

Julian

Jun 14 '06 #2

P: n/a
Christian Blackburn wrote on 14 jun 2006 in comp.lang.javascript:
I would like to use a caesar cipher on the mailto: links in my website
to prevent crawlers from farming the e-mails off my site. Can someone
tell me the equivalents to Chr() and Asc() from Visual Basic, but for
JavaScript? Chr(n) returns the character based on the numeric ASCII
code. For example
Chr(34) --> "
Asc(") --> 34


This is realy something you should be able to look up in the specs.

<http://msdn.microsoft.com/library/en-us/dnanchor/html/scriptinga.asp>

Or the ECMA equivalent.

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

These are not equivalent, as their use is much more versatile:

var test = String.fromCharCode(34);

var test = '"'.charCodeAt(0)


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 14 '06 #3

P: n/a
Hi Julian,

Thank you so much for your help and the examples. I really appreciate
it.

Cheers,
Christian Blackburn

Jun 14 '06 #4

P: n/a
Hi Evertjan,

Thanks for sending me the JScript Help URL. I may reference that from
time to time, but in general I prefer to use Netscape's JavaScript for
compatibility reasons. I'll agree that the command is more versatile.
However, for my purposes it's more or less the same :).

Cheers,
Christian Blackburn

Evertjan. wrote:
Christian Blackburn wrote on 14 jun 2006 in comp.lang.javascript:
I would like to use a caesar cipher on the mailto: links in my website
to prevent crawlers from farming the e-mails off my site. Can someone
tell me the equivalents to Chr() and Asc() from Visual Basic, but for
JavaScript? Chr(n) returns the character based on the numeric ASCII
code. For example
Chr(34) --> "
Asc(") --> 34


This is realy something you should be able to look up in the specs.

<http://msdn.microsoft.com/library/en-us/dnanchor/html/scriptinga.asp>

Or the ECMA equivalent.

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

These are not equivalent, as their use is much more versatile:

var test = String.fromCharCode(34);

var test = '"'.charCodeAt(0)


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Jun 14 '06 #5

P: n/a
JRS: In article <11**********************@c74g2000cwc.googlegroups .com>
, dated Tue, 13 Jun 2006 23:26:09 remote, seen in
news:comp.lang.javascript, Christian Blackburn <christian.Blackburn@Yaho
o.com> posted :
Hi Gang,

I would like to use a caesar cipher on the mailto: links in my website
to prevent crawlers from farming the e-mails off my site. Can someone
tell me the equivalents to Chr() and Asc() from Visual Basic, but for
JavaScript? Chr(n) returns the character based on the numeric ASCII
code. For example
Chr(34) --> "
Asc(") --> 34


The following, from <URL:http://www.merlyn.demon.co.uk/problems.htm>,
implements a specific Caesar cipher.

function R13(A) {
if (A> 96 && A<110) return A+13
if (A>109 && A<123) return A-13
if (A> 64 && A< 78) return A+13
if (A> 77 && A< 91) return A-13
return A }

function Rot13(X) { var j, Y = ""
for (j=0 ; j < X.length ; j++)
Y += String.fromCharCode(R13(X.charCodeAt(j)))
return Y }
Another approach, for later browsers, could be to use

S1 = S0.replace(/[a-z]/gi, XXX)

where XXX is a function resembling R13.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jun 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.