10 examples of 'nodejs buffer to base64' in JavaScript

Every line of 'nodejs buffer to base64' 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
46export function bufferToBase64(buffer) {
47 let ret = "";
48
49 let bytes = new Uint8Array(buffer);
50 let length = bytes.byteLength - (bytes.byteLength % 3);
51
52 /* eslint-disable no-bitwise */
53 for (let i = 0; i < length; i = i + 3) {
54 let bits = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
55 let enc1 = (bits >> 18) & 63;
56 let enc2 = (bits >> 12) & 63;
57 let enc3 = (bits >> 6) & 63;
58 let enc4 = bits & 63;
59 ret += BASE64[enc1] + BASE64[enc2] + BASE64[enc3] + BASE64[enc4];
60 }
61
62 length = bytes.byteLength;
63
64 if (length % 3 === 1) {
65 let bits = bytes[length - 1];
66 let enc1 = (bits >> 2) & 63;
67 let enc2 = (bits << 4) & 63;
68 ret += BASE64[enc1] + BASE64[enc2] + "==";
69 } else if (length % 3 === 2) {
70 let bits = (bytes[length - 2] << 8) | bytes[length - 1];
71 let enc1 = (bits >> 10) & 63;
72 let enc2 = (bits >> 4) & 63;
73 let enc3 = (bits << 2) & 63;
74 ret += BASE64[enc1] + BASE64[enc2] + BASE64[enc3] + "=";
75 }
76 /* eslint-enable no-bitwise */
77
78 return ret;
79}
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

11export function bufferToBase64(buffer: Uint8Array): string {
12 return Buffer.from(buffer).toString('base64');
13}
2function arrayBufferToBase64(buffer) {
3 return buffer.toString('binary');
4}
10function bufferToBase64 (buffer) {
11 var binary = ''
12 var bytes = new Uint8Array(buffer)
13 for (var len = bytes.byteLength, i = 0; i < len; i++) {
14 binary += String.fromCharCode(bytes[i])
15 }
16 return 'data:image/jpg;base64,' + window.btoa(binary)
17}
77function bufferToBase64(buffer: ArrayBuffer): string {
78 let binary = '';
79 let bytes = new Uint8Array(buffer);
80 let len = bytes.byteLength;
81 for (let index = 0; index < len; index++) {
82 binary += String.fromCharCode(bytes[index]);
83 }
84 return window.btoa(binary);
85}
604function bufferToBase64 (buffer) {
605
606 var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
607
608 var bytes = buffer, i, len = bytes.length, base64 = "";
609
610 for (i = 0; i < len; i+=3) {
611 base64 += chars[bytes[i] >> 2];
612 base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
613 base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
614 base64 += chars[bytes[i + 2] & 63];
615 }
616
617 if ((len % 3) === 2) {
618 base64 = base64.substring(0, base64.length - 1) + "=";
619 } else if (len % 3 === 1) {
620 base64 = base64.substring(0, base64.length - 2) + "==";
621 }
622
623 return base64
624}
10export function bufferToBase64(buffer) {
11 return btoa(String.fromCharCode(...new Uint8Array(buffer)));
12}
40function toBase64(buffer) {
41 let binary = '';
42 const bytes = new Uint8Array(buffer);
43 const len = bytes.byteLength;
44 for (let i = 0; i < len; i++) {
45 binary += String.fromCharCode(bytes[i]);
46 }
47 return window.btoa(binary);
48}
49function arrayBufferToBase64( buffer ) {
50 var binary = '';
51 var bytes = new Uint8Array( buffer );
52 var len = bytes.byteLength;
53 for (var i = 0; i < len; i++) {
54 binary += String.fromCharCode( bytes[ i ] );
55 }
56 return window.btoa( binary );
57}
98function arrayBufferToBase64(buffer) {
99 let binary = '';
100 const bytes = new Uint8Array(buffer);
101 const len = bytes.byteLength;
102 for (var i = 0; i < len; i++) {
103 binary += String.fromCharCode(bytes[i]);
104 }
105
106 return window.btoa(binary);
107}

Related snippets