Every line of 'javascript encode 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.
93 private 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 }
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
23 function encode (input) { 24 var output = '', 25 chr1, chr2, chr3, 26 enc1, enc2, enc3, enc4, 27 i = 0; 28 29 while (i < input.length) { 30 chr1 = input.charCodeAt(i++); 31 chr2 = input.charCodeAt(i++); 32 chr3 = input.charCodeAt(i++); 33 34 enc1 = chr1 >> 2; 35 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 36 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 37 enc4 = chr3 & 63; 38 39 if (isNaN(chr2)) { 40 enc3 = enc4 = 64; 41 } else if (isNaN(chr3)) { 42 enc4 = 64; 43 } 44 45 output = output + 46 keyStr.charAt(enc1) + 47 keyStr.charAt(enc2) + 48 keyStr.charAt(enc3) + 49 keyStr.charAt(enc4); 50 } 51 52 return output; 53 }
20 function 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 }
48 function 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 }
22 static base64Encode(str) { 23 let output = ''; 24 const keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; 25 let chr1, chr2, chr3, enc1, enc2, enc3, enc4; 26 let i = 0; 27 str = Encoder._utf8_encode(str); 28 while (i < str.length) { 29 chr1 = str.charCodeAt(i++); 30 chr2 = str.charCodeAt(i++); 31 chr3 = str.charCodeAt(i++); 32 enc1 = chr1 >> 2; 33 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 34 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 35 enc4 = chr3 & 63; 36 if (isNaN(chr2)) { 37 enc3 = enc4 = 64; 38 } else if (isNaN(chr3)) { 39 enc4 = 64; 40 } 41 output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4); 42 } 43 return output; 44 }
38 encode(input: string) { 39 const inputUtf8Arr = this.stringToUtf8Arr(encodeURIComponent(input)); 40 return this.base64EncArr(inputUtf8Arr); 41 }
281 function 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 };
49 function b64EncodeUnicode(str) { 50 return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { 51 return String.fromCharCode('0x' + p1); 52 })); 53 }
61 function 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 }
33 export function encode(bytes) { 34 let base64 = ''; 35 for (let i = 0; i < bytes.length; i += 3) { 36 base64 += chars[bytes[i] >> 2]; 37 base64 += chars[(bytes[i] & 3) << 4 | bytes[i + 1] >> 4]; 38 base64 += chars[(bytes[i + 1] & 15) << 2 | bytes[i + 2] >> 6]; 39 base64 += chars[bytes[i + 2] & 63]; 40 } 41 if (bytes.length % 3 === 2) { 42 base64 = base64.substring(0, base64.length - 1) + '='; 43 } else if (bytes.length % 3 === 1) { 44 base64 = base64.substring(0, base64.length - 2) + '=='; 45 } 46 return base64; 47 }