Skip to content

Commit 0964105

Browse files
committed
实现utf8解码
1 parent 0307971 commit 0964105

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

utf8_decode.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @description 实现utf8解码
3+
* @author 未知
4+
*/
5+
6+
function utf8_decode(str_data){
7+
var tmp_arr = [],i = 0,ac = 0,c1 = 0,c2 = 0,c3 = 0;str_data += '';
8+
while (i < str_data.length) {
9+
c1 = str_data.charCodeAt(i);
10+
if (c1 < 128) {
11+
tmp_arr[ac++] = String.fromCharCode(c1);
12+
i++;
13+
} else if (c1 > 191 && c1 < 224) {
14+
c2 = str_data.charCodeAt(i + 1);
15+
tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
16+
i += 2;
17+
} else {
18+
c2 = str_data.charCodeAt(i + 1);
19+
c3 = str_data.charCodeAt(i + 2);
20+
tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
21+
i += 3;
22+
}
23+
}
24+
return tmp_arr.join('');
25+
}

0 commit comments

Comments
 (0)