Following is an algorithm I found on Salesforce Community. It converts a 15 digit salesforce Id to the corresponding 18 digit Salesforce Id. This is pretty helpful in onclick JavaScript codes and S-Controls where merge fields normally return only 15 digits. Also, although the code is in JavaScript, it can be easily converted to any other coding language code.

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; }