Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q424 How do you convert a character to it's ascii value without using the V4 browser compatible charCodeAt()?

You are here: irt.org | FAQ | JavaScript | Text | Q424 [ previous next ]

The following almost works perfectly apart from 127-159:

<script language="JavaScript"><!--
function myCharCodeAt(text,position) {
    var tmp = text.substring(position,position+1);
    for (var i=1;i<=255;i++) {
        if (unescape('%' + i.toString(16)) == tmp)
            return i;
    }
    return 0;
}

var output = '<Table border="1"><tr><td>i<\/td><td>%i<\/td><td>$i<\/td><td>$i.charCodeAt(0)<\/td><td>myCharCodeAt($i,0)<\/td><\/tr>'
for (var i=1;i<=255;i++)
    output += '<tr><td>' + i + '<\/td><TD>' + i.toString(16) + '<\/td><td>' + unescape('%' + i.toString(16)) + '<\/td><td>' + unescape('%' + i.toString(16)).charCodeAt(0) + '<\/td><td>' + myCharCodeAt(unescape('%' + i.toString(16)),0) + '<\/td><\/tr>';

output += '<\/table>';

document.write(output);
//--></script>

Which can be tweaked to:

<script language="JavaScript"><!--
function myCharCodeAt(text,position) {
    var tmp = text.substring(position,position+1);
    for (var i=1;i<127;i++) {
        if (unescape('%' + i.toString(16)) == tmp)
            return i;
    }
    for (var i=160;i<=255;i++) {
        if (unescape('%' + i.toString(16)) == tmp)
            return i;
    }
    return 65533;
}

var output = '<table border="1"><tr><td>i<\/td><td>%i<\/td><td>$i<\/td><td>$i.charCodeAt(0)<\/td><td>myCharCodeAt($i,0)<\/td><\/tr>'
for (var i=1;i<=255;i++)
    output += '<tr><td>' + i + '<\/td><td>' + i.toString(16) + '<\/td><td>' + unescape('%' + i.toString(16)) + '<\/td><td>' + unescape('%' + i.toString(16)).charCodeAt(0) + '<\/td><td>' + myCharCodeAt(unescape('%' + i.toString(16)),0) + '<\/td><\/tr>';

output += '<\/table>';

document.write(output);
//--></script>

And if you need a faster version:

<script language="JavaScript"><!--
var chr = new Array();

for (var i=1;i<127;i++)
    chr[i]=unescape('%' + i.toString(16));
for (var i=127;i<=159;i++)
    chr[i]=65533;
for (var i=160;i<=255;i++)
    chr[i]=unescape('%' + i.toString(16));

function myCharCodeAt(text,position) {
    var tmp = text.substring(position,position+1);
    for (var i=1;i<=255;i++) {
        if (chr[i] == tmp)
            return i;
    }
    return 65533;
}

var output = '<table border="1"><tr><td>i<\/td><td>%i<\/td><td>$i<\/td><td>$i.charCodeAt(0)<\/td><td>myCharCodeAt($i,0)<\/td><\/tr>'
for (var i=1;i<=255;i++)
    output += '<tr><td>' + i + '<\/td><td>' + i.toString(16) + '<\/td><td>' + unescape('%' + i.toString(16)) + '<\/td><td>' + unescape('%' + i.toString(16)).charCodeAt(0) + '<\/td><td>' + myCharCodeAt(unescape('%' + i.toString(16)),0) + '<\/td><\/tr>';

output += '<\/table>';

document.write(output);
//--></script>

And an even faster version:

<script language="JavaScript"><!--
var chr = new Array();
var chrs = '';

for (var i=1;i<127;i++) {
    chr[i]=i;
    chrs += unescape('%' + i.toString(16));
}
for (var i=127;i<=159;i++) {
    chr[i]=65533;
    chrs += unescape('%' + i.toString(16));
}
for (var i=160;i<=255;i++) {
    chr[i]=i;
    chrs += unescape('%' + i.toString(16));
}

function myCharCodeAt(text,position) {
    return chr[chrs.indexOf(text.substring(position,position+1))+1];
}

var output = '<table border="1"><tr><td>i<\/td><td>%i<\/td><td>$i<\/td><td>$i.charCodeAt(0)<\/td><td>myCharCodeAt($i,0)<\/td><\/tr>'
for (var i=1;i<=255;i++)
    output += '<tr><td>' + i + '<\/td><td>' + i.toString(16) + '<\/td><td>' + unescape('%' + i.toString(16)) + '<\/td><td>' + unescape('%' + i.toString(16)).charCodeAt(0) + '<\/td><td>' + myCharCodeAt(unescape('%' + i.toString(16)),0) + '<\/td><\/tr>';

output += '<\/table>';

document.write(output);
//--></script>

Feedback on 'Q424 How do you convert a character to it's ascii value without using the V4 browser compatible charCodeAt()?'

©2018 Martin Webb