Percent encoding for UTF-8

Posted in Technicalon Apr 20, 2010

Warning: This post is not for those who don’t fancy a little bit of computer science.

I was recently looking for some Javascript code that would escape a filename so that a file with international character in the name could be uploaded to Google Docs through my GDocsUploader program.  With some help of some internet searching, this is what I came up with.  This function will take a UTF-8 string and replace non-ascii characters with percent encoded unicode values.

I thought I would share this in case it might help someone else in the future.

function utf8_percent_encode(string) {
    string = string.replace(/\r\n/g,"\n");
    var encstr = "";
    for (var n = 0; n < string.length; n++) {
        var c = string.charCodeAt(n);
        if (c < 128) {
             encstr += String.fromCharCode(c);
        } else if((c > 127) && (c < 2048)) {
             encstr += "%" + ((c >> 6) | 192).toString(16);
             encstr += "%" + ((c & 63) | 128).toString(16);
        } else {
             encstr += "%" + ((c >> 12) | 224).toString(16);
             encstr += "%" + (((c >> 6) & 63) | 128).toString(16);
             encstr += "%" + ((c & 63) | 128).toString(16);
        }
    }
    return encstr;
}

One Comment

Sandy

April 21st, 2010 at 1:36 pm

So, I read the first line and was out…

Comment Form

Categories