fork download
  1. // Base64EnDe.cpp : アプリケーションのエントリ ポイントを定義します。
  2. //
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include <cstdint>
  7. #include <tuple>
  8.  
  9.  
  10. typedef std::vector<std::uint8_t> DType;
  11. const std::string Token = "ABCDEFGHIJKLNMOPQRSTUVWXYZabcdefghijklnmopqrstuvwxyz0123456789+/";
  12. const std::string Pad = "=";
  13.  
  14.  
  15.  
  16. std::string Base64Enc(const std::string& s) {
  17. std::uint64_t V1 = 0;
  18. std::uint64_t V2 = 0;
  19. std::uint64_t P = 0;
  20. DType D;
  21. std::string R;
  22.  
  23.  
  24. for (std::size_t i = 0; i < s.size(); i++) {
  25.  
  26. V1 = s[i];
  27. i++;
  28.  
  29.  
  30. P = (V1 >> 2) & 0x3f;
  31. D.push_back(P);
  32. V1 &= 3;
  33.  
  34. V2 = s[i];
  35. i++;
  36.  
  37. P = ((V1 << 4) & 0x3f) | ((V2 >> 4) & 0x3f);
  38. D.push_back(P);
  39. V1 = V2 - ((V2 >> 4) << 4);
  40.  
  41. V2 = s[i];
  42. i++;
  43.  
  44. P = ((V1 << 2) & 0x3f) | ((V2 >> 6) & 0x3f);
  45. D.push_back(P);
  46. V1 = V2 - ((V2 >> 6) << 6);
  47.  
  48. P = V1 & 0x3f;
  49. D.push_back(P);
  50. }
  51.  
  52. for (auto& o : D) {
  53. R += Token[o];
  54. }
  55. for (std::size_t i = 0; i < 4 - (R.size() % 4); i++) {
  56. R += Pad;
  57. }
  58.  
  59. return R;
  60. }
  61.  
  62.  
  63. int main()
  64. {
  65. std::string S;
  66.  
  67. S = Base64Enc("ABCDEFG");
  68. std::cout << S << std::endl;
  69. return 0;
  70. }
  71.  
Success #stdin #stdout 0s 4544KB
stdin
Standard input is empty
stdout
QUJDRUZH==