你的位置:首页 > Java教程

[Java教程]javascript 中 unicode 转 UTF8


 1 var unicodeToUTF8 = function (unicode) { 2   if (unicode >= 0x00000000 && unicode <= 0x0000007F) { 3     return unicode; 4   } 5   else if (unicode >= 0x00000080 && unicode <= 0x000007FF) { 6     var r1 = (((unicode & 0x7C0) >> 6) | 0xC0) << 8; 7     var r2 = (unicode & 0x03F) | 0x80; 8     return r1 | r2; 9   }10   else if (unicode >= 0x00000800 && unicode <= 0x0000FFFF) {11     var r1 = (((unicode & 0xF000) >> 12) | 0xE0) << 16;12     var r2 = (((unicode & 0x0FC0) >> 6) | 0x80) << 8;13     var r3 = ((unicode & 0x003F) | 0x80);14     return r1 | r2 | r3;15   }16   else if (unicode >= 0x00010000 && unicode <= 0x0010FFFF) {17     var r1 = (((unicode & 0x1C0000) >> 18) | 0xE0) << 24;18     var r2 = (((unicode & 0x03F000) >> 12) | 0x80) << 16;19     var r3 = (((unicode & 0x000FC0) >> 6) | 0x80) << 8;20     var r4 = ((unicode & 0x00003F) | 0x80);21     return r1 | r2 | r3 | r4;22   }23   else {24     return false;25   }26 }

 

http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html