10 examples of 'javascript decode utf 8' in JavaScript

Every line of 'javascript decode utf 8' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
16static decode(data:Uint8Array):string{
17 if(data){
18 if(!UTF8.instance){
19 UTF8.instance = new UTF8();
20 }
21 return UTF8.instance.decode(data);
22 }
23 return null;
24}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

1function utf8_decode ( str_data ) {
2 // Converts a UTF-8 encoded string to ISO-8859-1
3 //
4 // version: 1004.2314
5 // discuss at: http://phpjs.org/functions/utf8_decode
6 // + original by: Webtoolkit.info (http://www.webtoolkit.info/)
7 // + input by: Aman Gupta
8 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
9 // + improved by: Norman "zEh" Fuchs
10 // + bugfixed by: hitwork
11 // + bugfixed by: Onno Marsman
12 // + input by: Brett Zamir (http://brett-zamir.me)
13 // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
14 // * example 1: utf8_decode('Kevin van Zonneveld');
15 // * returns 1: 'Kevin van Zonneveld'
16 var tmp_arr = [], i = 0, ac = 0, c1 = 0, c2 = 0, c3 = 0;
17
18 str_data += '';
19
20 while ( i < str_data.length ) {
21 c1 = str_data.charCodeAt(i);
22 if (c1 < 128) {
23 tmp_arr[ac++] = String.fromCharCode(c1);
24 i++;
25 } else if ((c1 > 191) && (c1 < 224)) {
26 c2 = str_data.charCodeAt(i+1);
27 tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
28 i += 2;
29 } else {
30 c2 = str_data.charCodeAt(i+1);
31 c3 = str_data.charCodeAt(i+2);
32 tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
33 i += 3;
34 }
35 }
36
37 return tmp_arr.join('');
38}
76function decode(s) {
77 s = s.replace(/%([EF][0-9A-F])%([89AB][0-9A-F])%([89AB][0-9A-F])/gi, ef)
78 .replace(/%([CD][0-9A-F])%([89AB][0-9A-F])/gi, cd)
79 .replace(/%([0-7][0-9A-F])/gi, o7);
80
81 return s;
82
83 function o7(code, hex) {
84 return String.fromCharCode(parseInt(hex,16));
85 }
86
87 function ef(code, hex1, hex2, hex3) {
88 var n, n3,
89 n1 = parseInt(hex1,16) - 0xE0,
90 n2 = parseInt(hex2,16) - 0x80;
91
92 if (n1 === 0 && n2 < 32)
93 return code;
94
95 n3 = parseInt(hex3,16)-0x80;
96 n = (n1<<12) + (n2<<6) + n3;
97
98 if (n > 0xFFFF)
99 return code;
100
101 return String.fromCharCode(n);
102 }
103
104 function cd(code, hex1, hex2) {
105 var n1, n2;
106
107 n1 = parseInt(hex1,16) - 0xC0;
108
109 if (n1 < 2)
110 return code;
111
112 n2 = parseInt(hex2,16) - 0x80;
113
114 return String.fromCharCode((n1<<6) + n2);
115 }
116}
200function utf8decode(byteString) {
201 byteArray = ucs2decode(byteString);
202 byteCount = byteArray.length;
203 byteIndex = 0;
204 var codePoints = [];
205 var tmp;
206 while ((tmp = decodeSymbol()) !== false) {
207 codePoints.push(tmp);
208 }
209 return ucs2encode(codePoints);
210}
38function decodeUnicode(str) {
39 // 注意: 如果出现 \\\u1234 就歇菜了
40 str = str.replace (/\\u([0-9a-f]{4})/g,
41 function ( a, n ) {
42 return String.fromCharCode(parseInt(n, 16));
43 });
44 return str;
45}
15function decode_utf8(s) {
16 return decodeURIComponent(escape(s));
17}
45b64DecodeUnicode(str) {
46 return decodeURIComponent(Array.prototype.map.call(atob(str), (c) => {
47 return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
48 }).join(''))
49}
93private utf8Encode(input: string): string {
94 var output = '';
95 var i = 0;
96 input = input.replace(/\r\n/g, '\n');
97
98 for (i = 0; i < input.length; i++) {
99
100 var c = input.charCodeAt(i);
101
102 if (c < 128) {
103 output += String.fromCharCode(c);
104 } else if ((c > 127) && (c < 2048)) {
105 output += String.fromCharCode((c >> 6) | 192);
106 output += String.fromCharCode((c & 63) | 128);
107 } else {
108 output += String.fromCharCode((c >> 12) | 224);
109 output += String.fromCharCode(((c >> 6) & 63) | 128);
110 output += String.fromCharCode((c & 63) | 128);
111 }
112 }
113
114 return output;
115}
42function decodeHtmlEntity(entity) {
43 if (entity.charAt(0) === '#') {
44 if (entity.length > 1) {
45 var ch1 = entity.charAt(1);
46 var codepoint;
47 if (ch1 === 'x' || ch1 === 'X') {
48 codepoint = parseInt(entity.substring(2), 16);
49 } else {
50 codepoint = parseInt(entity.substring(1), 10);
51 }
52 return isNaN(codepoint) ? null : String.fromCharCode(codepoint);
53 }
54 } else {
55 return HTML_ENTITIES_[entity.toLowerCase()];
56 }
57}
15function b64DecodeUnicode(str) {
16 return decodeURIComponent(Array.prototype.map.call(atob(str), function (c) {
17 return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
18 }).join(''));
19}

Related snippets