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

1 Answer

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

int main(void)
{
    std::string s = "432b2b2050726f67";
     
    size_t len = s.length();
     
    std::vector<unsigned int> 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(n);
    }
 
    for (const auto &i : v) 
        std::cout << i << " ";   

    return 0;
}
 
 
 
 
 
/*
run:
 
67 43 43 32 80 114 111 103 

*/

 



answered Sep 19, 2021 by avibootz

Related questions

1 answer 234 views
1 answer 146 views
1 answer 146 views
1 answer 178 views
1 answer 103 views
103 views asked Aug 20, 2023 by avibootz
1 answer 143 views
3 answers 240 views
...