有一段javascript算法,需要翻译为delphi,请高手解答
function utf8(wide)
{
    var c,s;
    var enc='';
    var i=0;
    while(i<wide.length)
    {
        c=wide.charCodeAt(i++);
        if(c>=0xDC00&&c<0xE000)continue;
        if(c>=0xD800&&c<0xDC00)
        {
            if(i>=wide.length)continue;
            s=wide.charCodeAt(i++);
            if(s<0xDC00||c>=0xDE00)continue;
            c=((c-0xD800)<<10)+(s-0xDC00)+0x10000;
        }
        if(c<0x80)enc+=String.fromCharCode(c);
        else if(c<0x800)enc+=String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
        else if(c<0x10000)enc+=String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
        else enc+=String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
    }
    return enc;
}
var hexchars="0123456789ABCDEF";
var okURIchars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
function toHex(n)
{
    return hexchars.charAt(n>>4)+hexchars.charAt(n&0xF);
}
function encodeURIComponentNew(s)
{
    var s=utf8(s);
    var c;
    var enc='';
    for(var i=0;i<s.length;i++)
    {
        if(okURIchars.indexOf(s.charAt(i))==-1)
        enc+='%'+toHex(s.charCodeAt(i));
        else
        enc+=s.charAt(i);
    }
    return enc;
}

解决方案 »

  1.   

    --来一小块。共参考
    procedure utf8(wide:wchar) 
        var c,s:wchar; 
        var enc :string; 
        var i :integer; begin 
      i :=0;
        while(i < legnth(wide) ) 
        begin 
            c :=wide.charCodeAt(inc(i) ); 
            if ((c>=$DC00) and c <$E000)) then
               continue; 
    //--自己来吧,主要是主意多字符问题
    //---------------to do
            if(c>=0xD800&&c <0xDC00) 
            { 
                if(i>=wide.length)continue; 
                s=wide.charCodeAt(i++); 
                if(s <0xDC00||c>=0xDE00)continue; 
                c=((c-0xD800) < <10)+(s-0xDC00)+0x10000; 
            } 
            if(c <0x80)enc+=String.fromCharCode(c); 
            else if(c <0x800)enc+=String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F)); 
            else if(c <0x10000)enc+=String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F)); 
            else enc+=String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F)); 
        } 
        return enc; 
      

  2.   

    可以直接使用delphi中的函数:AnsiToUtf8