7 examples of 'javascript utf 8 encoding' in JavaScript

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

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}
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};
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}
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}
7encoder: function utf7Encoder() {
8 return {
9 write: utf7EncoderWrite,
10 end: function() {},
11
12 iconv: options.iconv,
13 };
14},
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