C++ 31 Jan 2010 23:31:34

Snippet: Convert String to Hex


#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>

int main() {
    std::string str("abcABC123\xff\x01");
    std::stringstream ss;
    for (size_t i=0 ; i<str.length() ; ++i) {
        ss << std::setw(2) << std::setfill('0')
            << std::hex << (int(str[i])&0xFF);
    std::cout << ss.str();

Subscribe to the comments through RSS Feed

Leave a Reply

To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image