tool.js 673 B

123456789101112131415161718192021222324
  1. export function ArrayBufferToBase64 (buffer) {
  2. var binary = '';
  3. var bytes = new Uint8ClampedArray(buffer);
  4. for (var len = bytes.byteLength, i = 0; i < len; i++) {
  5. binary += String.fromCharCode(bytes[i]);
  6. }
  7. return btoa(binary);
  8. }
  9. export function Base64ToUint8ClampedArray(base64String) {
  10. const padding = '='.repeat((4 - base64String.length % 4) % 4);
  11. const base64 = (base64String + padding)
  12. .replace(/\-/g, '+')
  13. .replace(/_/g, '/');
  14. const rawData = atob(base64);
  15. const outputArray = new Uint8ClampedArray(rawData.length);
  16. for (let i = 0; i < rawData.length; ++i) {
  17. outputArray[i] = rawData.charCodeAt(i);
  18. }
  19. return outputArray;
  20. }