string转ascii

static std::string get_raw_string(std::string const& s)
{
    std::ostringstream out;
    out << '\"';
    out << std::hex;
    for (std::string::const_iterator it = s.begin(); it != s.end(); ++it)
    {
    // AND 0xFF will remove the leading "ff" in the output,
    // So that we could get "\xab" instead of "\xffab"
    out << "\\x" << (static_cast<short>(*it) & 0xff);
    }
    out << '\"';
    return out.str();
}

  

转10进制

std::string print_ascii(std::string str) { 
    std::string str_ascii("[");
    for (int i = 0; i < str.size(); ++i) {
        //if (isprint(str[i]) == 0) {
        std::stringstream oss;
        oss << int(str[i]);
        str_ascii +=  oss.str();
        str_ascii += "," ;
        //} else {
        //    char x[2];
        //    x[0] = str[i];
        //    x[1] = '\0';
        //    str_ascii += (std::string)x;
        //    str_ascii += "," ;
        //} 
    }   
    str_ascii += "]";
    return str_ascii;
}

  

posted @ 2017-05-25 10:08  我就是熊吉  阅读(1836)  评论(0编辑  收藏  举报