How to convert string to hexadecimal in C++

1 Answer

0 votes
#include <iostream>
#include <sstream>
#include <vector>
 
int main()
{
    std::string s = "C++ Programming";
 
    std::vector <std::string> v;
 
    for (size_t i = 0; i < s.size(); i++) {
        std::ostringstream oss;
        oss << std::hex << (int) s[i];
        v.push_back(oss.str());
    }

    std::string hex;
    
    for (const auto &str : v) 
        hex += str;
        
    std::cout << hex;        

    return 0;
}
 
 
 
 
 
/*
run:
 
432b2b2050726f6772616d6d696e67
 
*/

 



answered Sep 19, 2021 by avibootz

Related questions

1 answer 282 views
1 answer 208 views
1 answer 131 views
131 views asked Sep 17, 2021 by avibootz
1 answer 127 views
127 views asked Aug 25, 2021 by avibootz
1 answer 127 views
127 views asked Aug 24, 2021 by avibootz
1 answer 172 views
...