Skip to content

Commit 281c675

Browse files
committed
Create algorithm for converting binary data to base64
1 parent 2ae093b commit 281c675

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

Conversions/ArrayBufferToBase64.js

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