10 examples of 'javascript encoding utf 8' in JavaScript

Every line of 'javascript encoding 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
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};
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

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}
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}
898static _utf8Encode(string) {
899 if (string == null) return null;
900 if (dart.notNull(string[dartx.isEmpty])) return typed_data.Uint8List.new(0);
901 checkAscii: {
902 for (let i = 0; dart.notNull(i) < dart.notNull(string[dartx.length]); i = dart.notNull(i) + 1) {
903 if (dart.notNull(string[dartx.codeUnitAt](i)) >= 128) break checkAscii;
904 }
905 return string[dartx.codeUnits];
906 }
907 return UTF8.encode(string);
908}
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<l; n++)
21 {
22 c = string.charCodeAt(n);
23 if ( c < 128 )
24 output += CC(c);
25 else if ( (c > 127) && (c < 2048) )
26 output += CC((c >> 6) | 192) + CC((c & 63) | 128);
27 else
28 output += CC((c >> 12) | 224) + CC(((c >> 6) & 63) | 128) + CC((c & 63) | 128);
29 }
30 return output;
31}
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}
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}
90function Utf8Encode(string) {
91 // METEOR change:
92 // The webtoolkit.info version of this code added this
93 // Utf8Encode function (which does seem necessary for dealing
94 // with arbitrary Unicode), but the following line seems
95 // problematic:
96 //
97 // string = string.replace(/\r\n/g,"\n");
98 var utftext = '';
99
100 for (var n = 0; n < string.length; n++) {
101
102 var c = string.charCodeAt(n);
103
104 if (c < 128) {
105 utftext += String.fromCharCode(c);
106 }
107 else if((c > 127) && (c < 2048)) {
108 utftext += String.fromCharCode((c >> 6) | 192);
109 utftext += String.fromCharCode((c & 63) | 128);
110 }
111 else {
112 utftext += String.fromCharCode((c >> 12) | 224);
113 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
114 utftext += String.fromCharCode((c & 63) | 128);
115 }
116
117 }
118
119 return utftext;
120}
4function utf8Encode (string) {
5 string = string.replace(/\r\n/g, '\n')
6 var utftext = ''
7 for (var n = 0; n < string.length; n++) {
8 var c = string.charCodeAt(n)
9 if (c < 128) {
10 utftext += String.fromCharCode(c)
11 } else if(c > 127 && c < 2048) {
12 utftext += String.fromCharCode((c >> 6) | 192)
13 utftext += String.fromCharCode((c & 63) | 128)
14 } else {
15 utftext += String.fromCharCode((c >> 12) | 224)
16 utftext += String.fromCharCode(((c >> 6) & 63) | 128)
17 utftext += String.fromCharCode((c & 63) | 128)
18 }
19 }
20 return utftext
21}
49function utf8_encode ( argString ) {
50 var string = (argString+''); // .replace(/\r\n/g, "\n").replace(/\r/g, "\n");
51
52 var utftext = "";
53 var start, end;
54 var stringl = 0;
55
56 start = end = 0;
57 stringl = string.length;
58 for (var n = 0; n < stringl; n++) {
59 var c1 = string.charCodeAt(n);
60 var enc = null;
61
62 if (c1 < 128) {
63 end++;
64 } else if (c1 > 127 && c1 < 2048) {
65 enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
66 } else {
67 enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
68 }
69 if (enc !== null) {
70 if (end > start) {
71 utftext += string.substring(start, end);
72 }
73 utftext += enc;
74 start = end = n+1;
75 }
76 }
77
78 if (end > start) {
79 utftext += string.substring(start, string.length);
80 }
81
82 return utftext;
83}

Related snippets