How to split a hex string into a vector as characters in C++

1 Answer

0 votes
#include <iostream>
#include <sstream>
#include <vector>

int main(void)
{
    std::string s = "432b2b2050726f6772616d6d696e67";
     
    size_t len = s.length();
     
    std::vector<char> v;
     
    for(size_t i = 0; i < len; i += 2) {
        unsigned int n;   
        std::stringstream ss;
        ss << std::hex << s.substr(i, 2);
        ss >> n;
        
        v.push_back((char)n);
    }
 
    for (const auto &ch : v) 
        std::cout << ch;

    return 0;
}
 
 
 
 
 
/*
run:
 
C++ Programming

*/

 



answered Sep 19, 2021 by avibootz

Related questions

1 answer 149 views
1 answer 139 views
1 answer 150 views
1 answer 93 views
93 views asked Aug 20, 2023 by avibootz
1 answer 131 views
3 answers 209 views
3 answers 231 views
...