10 examples of 'javascript buffer to base64' in JavaScript

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

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}
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}
2function arrayBufferToBase64(buffer) {
3 return buffer.toString('binary');
4}
10export function bufferToBase64(buffer) {
11 return btoa(String.fromCharCode(...new Uint8Array(buffer)));
12}
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}
11export function bufferToBase64(buffer: Uint8Array): string {
12 return Buffer.from(buffer).toString('base64');
13}
73function _arrayBufferToBase64 (buffer)
74{
75 var binary = '';
76 var bytes = new Uint8Array(buffer);
77 var len = bytes.byteLength;
78 for (var i = 0; i < len; i++) {
79 binary += String.fromCharCode(bytes[i]);
80 }
81 return window.btoa(binary);
82}
63export function arrayBufferToBase64(buffer) {
64 var binary = '';
65 var bytes = new Uint8Array(buffer);
66 var len = bytes.byteLength;
67 for (var i = 0; i < len; i++) {
68 binary += String.fromCharCode(bytes[i]);
69 }
70 return btoa(binary);
71}
98function arrayBufferToBase64(buffer) {
99 var bytes = new Uint8Array(buffer);
100 var len = bytes.byteLength;
101 var binary = '';
102 for (var i = 0; i < len; i++)
103 binary += String.fromCharCode(bytes[i]);
104 return window.btoa(binary);
105}

Related snippets