Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Gb2312ToUtf8(s1) {
- var s = escape(s1)
- var sa = s.split('%')
- var retV = ''
- if (sa[0] != '') {
- retV = sa[0]
- }
- for (var i = 1; i < sa.length; i++) {
- if (sa[i].substring(0, 1) == 'u') {
- retV += this.Hex2Utf8(this.Str2Hex(sa[i].substring(1, 5)))
- if (sa[i].length) {
- retV += sa[i].substring(5)
- }
- } else {
- retV += unescape('%' + sa[i])
- if (sa[i].length) {
- retV += sa[i].substring(5)
- }
- }
- }
- return retV
- }
- function Utf8ToGb2312(str1) {
- var substr = ''
- var a = ''
- var b = ''
- var c = ''
- var i = -1
- i = str1.indexOf('%')
- if (i == -1) {
- return str1
- }
- while (i != -1) {
- if (i < 3) {
- substr = substr + str1.substr(0, i - 1)
- str1 = str1.substr(i + 1, str1.length - i)
- a = str1.substr(0, 2)
- str1 = str1.substr(2, str1.length - 2)
- if (parseInt('0x' + a) & (0x80 == 0)) {
- substr = substr + String.fromCharCode(parseInt('0x' + a))
- } else if (parseInt('0x' + a) & (0xe0 == 0xc0)) {
- //two byte
- b = str1.substr(1, 2)
- str1 = str1.substr(3, str1.length - 3)
- var widechar = (parseInt('0x' + a) & 0x1f) << 6
- widechar = widechar | (parseInt('0x' + b) & 0x3f)
- substr = substr + String.fromCharCode(widechar)
- } else {
- b = str1.substr(1, 2)
- str1 = str1.substr(3, str1.length - 3)
- c = str1.substr(1, 2)
- str1 = str1.substr(3, str1.length - 3)
- var widechar = (parseInt('0x' + a) & 0x0f) << 12
- widechar = widechar | ((parseInt('0x' + b) & 0x3f) << 6)
- widechar = widechar | (parseInt('0x' + c) & 0x3f)
- substr = substr + String.fromCharCode(widechar)
- }
- } else {
- substr = substr + str1.substring(0, i)
- str1 = str1.substring(i)
- }
- i = str1.indexOf('%')
- }
- return substr + str1
- }
Add Comment
Please, Sign In to add comment