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 392 views
1 answer 307 views
1 answer 228 views
228 views asked Sep 17, 2021 by avibootz
1 answer 242 views
242 views asked Aug 25, 2021 by avibootz
1 answer 228 views
228 views asked Aug 24, 2021 by avibootz
...