10 examples of 'javascript convert string to utf 8' in JavaScript

Every line of 'javascript convert string to 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
281function Utf8Encode(string) {
282 string = string.replace(/\r\n/g,"\n");
283 var utftext = "";
284
285 for (var n = 0; n < string.length; n++) {
286
287 var c = string.charCodeAt(n);
288
289 if (c < 128) {
290 utftext += String.fromCharCode(c);
291 }
292 else if((c > 127) && (c < 2048)) {
293 utftext += String.fromCharCode((c >> 6) | 192);
294 utftext += String.fromCharCode((c & 63) | 128);
295 }
296 else {
297 utftext += String.fromCharCode((c >> 12) | 224);
298 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
299 utftext += String.fromCharCode((c & 63) | 128);
300 }
301
302 }
303
304 return utftext;
305};
43static utf8StrToBytes(str) {
44 return new TextEncoder("utf-8").encode(str);
45}
20function Utf8Encode(string) {
21 string = string.replace(/\r\n/g,"\n");
22 var utftext = "";
23
24 for (var n = 0; n < string.length; n++) {
25
26 var c = string.charCodeAt(n);
27
28 if (c < 128) {
29 utftext += String.fromCharCode(c);
30 }
31 else if((c > 127) && (c < 2048)) {
32 utftext += String.fromCharCode((c >> 6) | 192);
33 utftext += String.fromCharCode((c & 63) | 128);
34 }
35 else {
36 utftext += String.fromCharCode((c >> 12) | 224);
37 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
38 utftext += String.fromCharCode((c & 63) | 128);
39 }
40
41 }
42
43 return utftext;
44}
16function utf8_encode( string )
17{
18 string = string.replace(hex_re, "\x0a");
19 var output = '', n, l, c, CC = String.fromCharCode;
20 for (n=0,l=string.length; n 127) && (c < 2048) )
21 output += CC((c >> 6) | 192) + CC((c & 63) | 128);
22 else
23 output += CC((c >> 12) | 224) + CC(((c >> 6) & 63) | 128) + CC((c & 63) | 128);
24 }
25 return output;
26}
102function utf16to8(str) {
103 var out, i, len, c;
104 out = "";
105 len = str.length;
106 for (i = 0; i < len; i++) {
107 c = str.charCodeAt(i);
108 if ((c >= 0x0001) && (c <= 0x007F)) {
109 out += str.charAt(i);
110 } else if (c > 0x07FF) {
111 out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
112 out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
113 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
114 } else {
115 out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
116 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
117 }
118 }
119 return out;
120}
99function ConvertFromUtf32(i) {
100 return $jsilcore.fromCharCode(i);
101}
48function encodeUnicode(str) {
49 var res = [];
50 for(var i = 0; i < str.length; i ++ ) {
51 var c = str.charCodeAt(i);
52 if (c > 0x7F) {
53 res[i] = "\\u"+c.toString(16);
54 } else {
55 res[i] = str [i];
56 }
57 }
58 return res.join("");
59}
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}
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}
61function utf8Eencode(string) {
62 string = string.replace(/\r\n/g, "\n");
63 var utftext = "";
64
65 for (var n = 0; n < string.length; n++) {
66
67 var c = string.charCodeAt(n);
68
69 if (c < 128) {
70 utftext += String.fromCharCode(c);
71 }
72 else if ((c > 127) && (c < 2048)) {
73 utftext += String.fromCharCode((c >> 6) | 192);
74 utftext += String.fromCharCode((c & 63) | 128);
75 }
76 else {
77 utftext += String.fromCharCode((c >> 12) | 224);
78 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
79 utftext += String.fromCharCode((c & 63) | 128);
80 }
81
82 }
83
84 return utftext;
85}

Related snippets