Guest User

Untitled

a guest
Oct 17th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. function base64Encode(inputStr) {
  2. var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  3. var outputStr = "";
  4. var i = 0;
  5.  
  6. while (i < inputStr.length)
  7. {
  8. //all three "& 0xff" added below are there to fix a known bug
  9. //with bytes returned by xhr.responseText
  10. var byte1 = inputStr.charCodeAt(i++) & 0xff;
  11. var byte2 = inputStr.charCodeAt(i++) & 0xff;
  12. var byte3 = inputStr.charCodeAt(i++) & 0xff;
  13.  
  14. var enc1 = byte1 >> 2;
  15. var enc2 = ((byte1 & 3) << 4) | (byte2 >> 4);
  16.  
  17. var enc3, enc4;
  18. if (isNaN(byte2))
  19. {
  20. enc3 = enc4 = 64;
  21. }
  22. else
  23. {
  24. enc3 = ((byte2 & 15) << 2) | (byte3 >> 6);
  25. if (isNaN(byte3))
  26. {
  27. enc4 = 64;
  28. }
  29. else
  30. {
  31. enc4 = byte3 & 63;
  32. }
  33. }
  34.  
  35. outputStr += b64.charAt(enc1) + b64.charAt(enc2) + b64.charAt(enc3) + b64.charAt(enc4);
  36. }
  37.  
  38. return outputStr;
  39. }
Add Comment
Please, Sign In to add comment