Last active
January 14, 2023 02:24
-
-
Save xl1/0b24fb2e61c39448a38d3c90b6c6fe3b to your computer and use it in GitHub Desktop.
binary string, ArrayBuffer, Blob, base64 string の相互変換
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const convert = { | |
binary: { | |
toArrayBuffer(binary) { | |
return Uint8Array.from(binary, s => s.charCodeAt(0)).buffer; | |
}, | |
toBlob(binary, mimeType) { | |
return new Blob([binary], { type: mimeType }); | |
}, | |
toBase64: binary => btoa(binary) | |
}, | |
arrayBuffer: { | |
toBinary(ab) { | |
return String.fromCharCode(...new Uint8Array(ab)); | |
}, | |
toBlob(ab, mimeType) { | |
return new Blob([ab], { type: mimeType }); | |
}, | |
toBase64(ab) { | |
return btoa(this.toBinary(ab)); | |
}, | |
}, | |
blob: { | |
_readAsync(blob, mode) { | |
return new Promise((resolve, reject) => { | |
const reader = new FileReader(); | |
reader.onload = e => resolve(reader.result); | |
reader.onerror = reject; | |
reader[mode](blob); | |
}); | |
}, | |
toBinaryAsync(blob) { | |
return this._readAsync(blob, 'readAsBinaryString'); | |
}, | |
toArrayBufferAsync(blob) { | |
return this._readAsync(blob, 'readAsArrayBuffer'); | |
}, | |
toBase64Async(blob) { | |
return this._readAsync(blob, 'readAsDataURL').then(dataUri => | |
dataUri.replace(/data:[^;]+;base64,/, '') | |
); | |
} | |
}, | |
base64: { | |
toBinary: b64 => atob(b64), | |
toArrayBuffer: b64 => convert.binary.toArrayBuffer(atob(b64)), | |
toBlob: (b64, mimeType) => convert.binary.toBlob(atob(b64), mimeType) | |
} | |
}; | |
function arrayBufferEqual(ab1, ab2) { | |
if (ab1.byteLength !== ab2.byteLength) return false; | |
const a1 = new Uint8Array(ab1); | |
const a2 = new Uint8Array(ab2); | |
for (let i = 0; i < a1.length; i++) { | |
if (a1[i] !== a2[i]) return false; | |
} | |
return true; | |
} | |
(async function test() { | |
const binary = 'ABC'; | |
const arrayBuffer = new Uint8Array([65, 66, 67]).buffer; | |
const base64 = 'QUJD'; | |
console.assert(arrayBufferEqual(arrayBuffer, convert.binary.toArrayBuffer(binary))); | |
console.assert(base64 === convert.binary.toBase64(binary)); | |
console.assert(binary === convert.arrayBuffer.toBinary(arrayBuffer)); | |
console.assert(base64 === convert.arrayBuffer.toBase64(arrayBuffer)); | |
console.assert(binary === convert.base64.toBinary(base64)); | |
console.assert(arrayBufferEqual(arrayBuffer, convert.base64.toArrayBuffer(base64))); | |
const blobFromBinary = convert.binary.toBlob(binary, 'application/octet-stream'); | |
console.assert(arrayBufferEqual(arrayBuffer, await convert.blob.toArrayBufferAsync(blobFromBinary))); | |
const blobFromArrayBuffer = convert.arrayBuffer.toBlob(arrayBuffer, 'application/octet-stream'); | |
console.assert(base64 === await convert.blob.toBase64Async(blobFromArrayBuffer)); | |
const blobFromBase64 = convert.base64.toBlob(base64, 'application/octet-stream'); | |
console.assert(binary === await convert.blob.toBinaryAsync(blobFromBase64)); | |
console.log('test done'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment