Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <iomanip>
- #include <sstream>
- #include <cstring>
- using namespace std;
- class Base64Decoder {
- private:
- static constexpr const char* base64_chars =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "abcdefghijklmnopqrstuvwxyz"
- "0123456789+/";
- public:
- static std::string base64_to_hex(const std::string& base64_input) {
- std::vector<unsigned char> decoded_bytes;
- int val = 0, valb = -8;
- for (unsigned char c : base64_input) {
- if (c == '=') break;
- const char* pos = std::strchr(base64_chars, c);
- if (pos == nullptr) continue; // Ignorar caracteres no válidos
- val = (val << 6) + (pos - base64_chars);
- valb += 6;
- if (valb >= 0) {
- decoded_bytes.push_back(char((val >> valb) & 0xFF));
- valb -= 8;
- }
- }
- std::stringstream hex_stream;
- hex_stream << std::hex << std::setfill('0');
- for (unsigned char byte : decoded_bytes) {
- hex_stream << std::setw(2) << static_cast<int>(byte);
- }
- return hex_stream.str();
- }
- // conversion chaîne hexadécimale en base64 p.e '060200000000' ->BgIAAAAA
- // Fonction pour convertir une chaîne hexadécimale en vector bytes
- std::vector<uint8_t> hex_string_to_vector(const std::string& hex_string) {
- std::vector<uint8_t> bytes;
- for (size_t i = 0; i < hex_string.length(); i += 2) {
- std::string byteString = hex_string.substr(i, 2);
- uint8_t byte = (uint8_t) std::stoi(byteString, nullptr, 16);
- bytes.push_back(byte);
- }
- return bytes;
- }
- };
- int main() {
- //Lire entree Base64
- /*
- std::string base64_input;
- std::cout << "Entrez la chaîne Base64: ";
- std::cin >> base64_input;
- */
- string base64_input="BQAA";
- Base64Decoder base64;//Instance Base64Decoder
- string hex_string = base64. base64_to_hex(base64_input);//Base64 to HexString
- cout <<base64_input<<" : "<<hex_string<<endl;//Debug
- //Création d'un vecteur hexadécimal uint_8
- std::vector<uint8_t> vector_hex;
- vector_hex =base64.hex_string_to_vector(hex_string);//Conversion string hex to vector uint_8
- for (uint8_t const & elem:vector_hex){
- cout << "-> 0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(elem) << std::dec << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment