zoukankan      html  css  js  c++  java
  • 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] = '';
            //    str_ascii += (std::string)x;
            //    str_ascii += "," ;
            //} 
        }   
        str_ascii += "]";
        return str_ascii;
    }
    

      

  • 相关阅读:
    0055. Jump Game (M)
    0957. Prison Cells After N Days (M)
    Java
    Java
    Java桌面应用程序打包
    JavaGUI练习
    Java贪吃蛇小游戏
    Java GUI编程
    Java异常处理机制
    抽象类与接口
  • 原文地址:https://www.cnblogs.com/xiongji/p/6902277.html
Copyright © 2011-2022 走看看