Created
December 1, 2012 05:37
-
-
Save CatTail/4180701 to your computer and use it in GitHub Desktop.
Javascript: encoding conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// escape don't: * @ – _ + . / | |
// encodeURI don't: ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) # | |
// encodeURIComponent don't: - _ . ! ~ * ' ( ) | |
/* | |
// 部分转义 | |
// text -> unicode | |
unicode = escape(text); | |
// unicode -> text | |
text = unescape(unicode); | |
// text -> utf8 | |
utf8 = encodeURIComponent(text); | |
// utf8 -> text | |
text = decodeURIComponent(utf8); | |
// unicode -> utf8 | |
utf8 = encodeURIComponent(unescape(unicode)); | |
// utf8 -> unicode | |
unicode = escape(decodeURIComponent(utf8)); | |
*/ | |
// 全部转义 | |
// text -> unicode | |
var toUnicode = function(str) { | |
var buf = []; | |
for (var i=str.length-1;i>=0;i--) { | |
buf.unshift( '%u' + str.charCodeAt(i).toString(16) ); | |
} | |
return buf.join(''); | |
}; | |
// unicode -> text | |
var fromUnicode = function(unicode) { | |
return unicode.replace(/%u([0-9a-fA-F]+)/g, function(match, hex) { | |
return String.fromCharCode(parseInt(hex, 16)); | |
}); | |
}; | |
// unicode -> utf8 | |
var unicode2utf8 = function(unicode) { | |
return unicode.replace(/%u([0-9a-fA-F]+)/g, function(match, hex) { | |
var utf8CharCodes = []; | |
c = parseInt(hex, 16); | |
if (c < 128) { | |
utf8CharCodes.push(c); | |
} else if (c < 2048) { | |
utf8CharCodes.push((c >> 6) | 192, (c & 63) | 128); | |
} else if (c < 65536) { | |
utf8CharCodes.push((c >> 12) | 224, ((c >> 6) & 63) | 128, (c & 63) | 128); | |
} else { | |
utf8CharCodes.push((c >> 18) | 240, ((c >> 12) & 63) | 128, ((c >> 6) & 63) | 128, (c & 63) | 128); | |
} | |
for (var i=utf8CharCodes.length-1;i>=0;i--) { | |
utf8CharCodes[i] = '%' + utf8CharCodes[i].toString(16); | |
} | |
return utf8CharCodes.join(''); | |
}); | |
}; | |
// utf8 -> unicode | |
var utf82unicode = function(utf8) { | |
var parts = utf8.split('%').slice(1), | |
i = 0, | |
buf = []; | |
while (i<parts.length) { | |
var part = parseInt(parts[i], 16); | |
if ((part & 128) === 0) { | |
buf.push(part); | |
i = i+1; | |
} else if ((part & 240) === 240) { | |
var part2 = parseInt(parts[i+1], 16); | |
var part3 = parseInt(parts[i+2], 16); | |
var part4 = parseInt(parts[i+3], 16); | |
buf.push( | |
((part & 7) << 18) + ((part2 & 63) << 12) + ((part3 & 63) << 6) + (part4 & 63) | |
); | |
i = i+4; | |
} else if ((part & 224) === 224) { | |
var part2 = parseInt(parts[i+1], 16); | |
var part3 = parseInt(parts[i+2], 16); | |
buf.push( | |
((part & 15) << 12) + ((part2 & 63) << 6) + (part3 & 63) | |
); | |
i = i+3; | |
} else if ((part & 192) === 192) { | |
var part2 = parseInt(parts[i+1], 16); | |
buf.push( | |
((part & 31) << 6) + (part2 & 63) | |
); | |
i = i+2; | |
} | |
} | |
for (var i=buf.length-1;i>=0;i--) { | |
buf[i] = '%u' + buf[i].toString(16); | |
} | |
return buf.join(''); | |
}; | |
// text -> utf8 | |
var toUtf8 = function(str){ | |
return unicode2utf8(toUnicode(str)); | |
}; | |
// utf8 -> text | |
var fromUtf8 = function(utf8) { | |
return fromUnicode(utf82unicode(utf8)); | |
}; | |
// text -> html entity | |
var toHtmlEntity = function(str) { | |
var buf = []; | |
for (var i=str.length-1;i>=0;i--) { | |
buf.unshift('&#'+str.charCodeAt(i)+';'); | |
} | |
return buf.join(''); | |
}; | |
// html entity -> text | |
var fromHtmlEntity = function(entities) { | |
return entities.replace(/&#(\d+);/g, function(match, dec) { | |
return String.fromCharCode(dec); | |
}); | |
}; | |
(function(){ | |
var str = 'JavaScript高级程序设计'; | |
var unicode = toUnicode(str); | |
var utf8 = toUtf8(str); | |
var entities = toHtmlEntity(str); | |
var utf82 = unicode2utf8(unicode); | |
var unicode2 = utf82unicode(utf8); | |
var str2 = fromUnicode(unicode); | |
var str3 = fromUtf8(utf8); | |
var str4 = fromHtmlEntity(entities); | |
console.log(str); | |
console.log(str2); | |
console.log(str3); | |
console.log(str4); | |
console.log(utf8); | |
console.log(utf82); | |
console.log(unicode); | |
console.log(unicode2); | |
console.log(entities); | |
// Output: | |
// JavaScript高级程序设计 | |
// JavaScript高级程序设计 | |
// JavaScript高级程序设计 | |
// JavaScript高级程序设计 | |
// %4a%61%76%61%53%63%72%69%70%74%e9%ab%98%e7%ba%a7%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1 | |
// %4a%61%76%61%53%63%72%69%70%74%e9%ab%98%e7%ba%a7%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1 | |
// %u4a%u61%u76%u61%u53%u63%u72%u69%u70%u74%u9ad8%u7ea7%u7a0b%u5e8f%u8bbe%u8ba1 | |
// %u4a%u61%u76%u61%u53%u63%u72%u69%u70%u74%u9ad8%u7ea7%u7a0b%u5e8f%u8bbe%u8ba1 | |
// JavaScript高级程序设计 | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment