Skip to content

Commit a83168c

Browse files
authored
chore: Merge pull request #756 from nibble-4bits/algorithm/bufferToBase64
Create algorithm for converting binary data to base64
2 parents fc0647e + 5d56140 commit a83168c

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

Conversions/ArrayBufferToBase64.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// About base64: https://en.wikipedia.org/wiki/Base64
2+
3+
/**
4+
* Converts an array of bytes to base64 encoding
5+
* @param {ArrayBuffer} binaryData An ArrayBuffer which represents an array of bytes
6+
* @returns {string} A string containing the base64 encoding of `binaryData`
7+
*/
8+
function bufferToBase64 (binaryData) {
9+
// The base64 encoding uses the following set of characters to encode any binary data as text
10+
const base64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
11+
// Every 3 bytes translates to 4 base64 characters, if we have less than 3 bytes we must append '=' chars as padding
12+
const padding = 3 - (binaryData.byteLength % 3)
13+
// Create an instance of Uint8Array, to read from the binaryData array buffer
14+
const byteView = new Uint8Array(binaryData)
15+
let result = ''
16+
17+
// Loop through all bytes in the buffer, in increments of 3 bytes
18+
for (let i = 0; i < byteView.byteLength; i += 3) {
19+
// Get the index for the next 4 base64 chars
20+
const char1 = (byteView[i] & 252) >> 2
21+
const char2 = ((byteView[i] & 3) << 4) + ((byteView[i + 1] & 240) >> 4)
22+
const char3 = ((byteView[i + 1] & 15) << 2) + ((byteView[i + 2] & 192) >> 6)
23+
const char4 = byteView[i + 2] & 63
24+
25+
result +=
26+
base64Table[char1] +
27+
base64Table[char2] +
28+
base64Table[char3] +
29+
base64Table[char4]
30+
}
31+
32+
// Add padding '=' chars if needed
33+
if (padding !== 3) {
34+
const paddedResult = result.slice(0, result.length - padding) + '='.repeat(padding)
35+
return paddedResult
36+
}
37+
38+
return result
39+
}
40+
41+
export { bufferToBase64 }
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { bufferToBase64 } from '../ArrayBufferToBase64'
2+
import { TextEncoder } from 'util'
3+
4+
describe('ArrayBufferToBase64', () => {
5+
it('should encode "Hello, world!" as "SGVsbG8sIHdvcmxkIQ=="', () => {
6+
const testString = 'Hello, world!'
7+
const encoder = new TextEncoder()
8+
const helloWorldBuffer = encoder.encode(testString)
9+
const result = bufferToBase64(helloWorldBuffer)
10+
expect(result).toBe('SGVsbG8sIHdvcmxkIQ==')
11+
})
12+
13+
it('should encode binary buffer [55,23,177,234,68,26,90] as "Nxex6kQaWg=="', () => {
14+
const testBuffer = new Uint8Array([55, 23, 177, 234, 68, 26, 90])
15+
const result = bufferToBase64(testBuffer)
16+
expect(result).toBe('Nxex6kQaWg==')
17+
})
18+
19+
it('should encode binary buffer [0,1,2,3,4,5,6,7,8,9] as "AAECAwQFBgcICQ=="', () => {
20+
const testBuffer = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
21+
const result = bufferToBase64(testBuffer)
22+
expect(result).toBe('AAECAwQFBgcICQ==')
23+
})
24+
})

0 commit comments

Comments
 (0)