You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
696 B
24 lines
696 B
|
8 months ago
|
|
||
|
|
export function ArrayBufferToBase64 (buffer) {
|
||
|
|
var binary = '';
|
||
|
|
var bytes = new Uint8ClampedArray(buffer);
|
||
|
|
for (var len = bytes.byteLength, i = 0; i < len; i++) {
|
||
|
|
binary += String.fromCharCode(bytes[i]);
|
||
|
|
}
|
||
|
|
return btoa(binary);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function Base64ToUint8ClampedArray(base64String) {
|
||
|
|
const padding = '='.repeat((4 - base64String.length % 4) % 4);
|
||
|
|
const base64 = (base64String + padding)
|
||
|
|
.replace(/\-/g, '+')
|
||
|
|
.replace(/_/g, '/');
|
||
|
|
|
||
|
|
const rawData = atob(base64);
|
||
|
|
const outputArray = new Uint8ClampedArray(rawData.length);
|
||
|
|
|
||
|
|
for (let i = 0; i < rawData.length; ++i) {
|
||
|
|
outputArray[i] = rawData.charCodeAt(i);
|
||
|
|
}
|
||
|
|
return outputArray;
|
||
|
|
}
|