10 examples of 'javascript utf8 encode' in JavaScript

Every line of 'javascript utf8 encode' 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

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}
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};
10function encodeUTF8 (str)
11{
12 return unescape(encodeURIComponent(str));
13}
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}
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}
47function utf8Encode(s) {
48 var buf = [];
49 var n = s.length;
50 for (var i = 0, j = 0; i < n; ++i, ++j) {
51 var codeUnit = s.charCodeAt(i);
52 if (codeUnit < 0x80) {
53 buf[j] = s.charAt(i);
54 }
55 else if (codeUnit < 0x800) {
56 buf[j] = String.fromCharCode(0xC0 | (codeUnit >> 6),
57 0x80 | (codeUnit & 0x3F));
58 }
59 else if (codeUnit < 0xD800 || codeUnit > 0xDFFF) {
60 buf[j] = String.fromCharCode(0xE0 | (codeUnit >> 12),
61 0x80 | ((codeUnit >> 6) & 0x3F),
62 0x80 | (codeUnit & 0x3F));
63 }
64 else {
65 if (i + 1 < n) {
66 var nextCodeUnit = s.charCodeAt(i + 1);
67 if (codeUnit < 0xDC00 && 0xDC00 <= nextCodeUnit && nextCodeUnit <= 0xDFFF) {
68 var rune = (((codeUnit & 0x03FF) << 10) | (nextCodeUnit & 0x03FF)) + 0x010000;
69 buf[j] = String.fromCharCode(
70 0xF0 | ((rune >> 18) &0x3F),
71 0x80 | ((rune >> 12) & 0x3F),
72 0x80 | ((rune >> 6) & 0x3F),
73 0x80 | (rune & 0x3F));
74 ++i;
75 continue;
76 }
77 }
78 throw new Error('Malformed string');
79 }
80 }
81 return buf.join('');
82}
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}
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}
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}

Related snippets