Here is one algorithm (copied verbatim below).
function normaliseSforceID( id) { // fluff up a 15 char id to return an 18 char id
if (id == null) return id;
id = id.replace(/\"/g, ''); // scrub quotes from this id
if (id.length != 15) {
//print('well, id is not 15, bye' + id + ' ' + id.length);
return null;
}
var suffix = "";
for (var i = 0; i < 3; i++) {
var flags = 0;
for (var j = 0; j < 5; j++) {
var c = id.charAt(i * 5 + j);
if (c >= 'A' && c < = 'Z') {
flags += 1 << j;
}
}
if (flags <= 25) {
suffix += "ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(flags);
} else {
suffix += "012345".charAt(flags-26);
}
}
return id + suffix;
}
Note that if you are trying to do this conversion on a Visualforce Page
you can just use "{!CASESAFEID(idValue)}"
.