首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转义C++字符串

转义C++字符串
EN

Stack Overflow用户
提问于 2010-03-10 22:26:51
回答 5查看 30.4K关注 0票数 18

将一个C++ std::string转换为另一个std::string的最简单方法是什么?

例如,对于两个字符0x61,0x01的字符串,结果字符串可能是"a\x01“或"a%01”。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-03-10 22:47:53

看看Boost的String Algorithm Library。你可以使用它的is_print分类器(连同它的运算符!重载)来挑选出不可打印的字符,它的find_format()函数可以用您想要的任何格式替换这些字符。

代码语言:javascript
复制
#include <iostream>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>

struct character_escaper
{
    template<typename FindResultT>
    std::string operator()(const FindResultT& Match) const
    {
        std::string s;
        for (typename FindResultT::const_iterator i = Match.begin();
             i != Match.end();
             i++) {
            s += str(boost::format("\\x%02x") % static_cast<int>(*i));
        }
        return s;
    }
};

int main (int argc, char **argv)
{
    std::string s("a\x01");
    boost::find_format_all(s, boost::token_finder(!boost::is_print()), character_escaper());
    std::cout << s << std::endl;
    return 0;
}
票数 12
EN

Stack Overflow用户

发布于 2010-03-10 23:01:31

假设执行字符集是ASCII码的超集,CHAR_BIT为8。对于OutIter,传递back_inserter (例如,向量或另一个字符串)、ostream_iterator或任何其他合适的输出迭代器。

代码语言:javascript
复制
template<class OutIter>
OutIter write_escaped(std::string const& s, OutIter out) {
  *out++ = '"';
  for (std::string::const_iterator i = s.begin(), end = s.end(); i != end; ++i) {
    unsigned char c = *i;
    if (' ' <= c and c <= '~' and c != '\\' and c != '"') {
      *out++ = c;
    }
    else {
      *out++ = '\\';
      switch(c) {
      case '"':  *out++ = '"';  break;
      case '\\': *out++ = '\\'; break;
      case '\t': *out++ = 't';  break;
      case '\r': *out++ = 'r';  break;
      case '\n': *out++ = 'n';  break;
      default:
        char const* const hexdig = "0123456789ABCDEF";
        *out++ = 'x';
        *out++ = hexdig[c >> 4];
        *out++ = hexdig[c & 0xF];
      }
    }
  }
  *out++ = '"';
  return out;
}
票数 9
EN

Stack Overflow用户

发布于 2017-07-09 01:41:06

假设“最简单的方法”意味着简短而又容易理解,而不依赖于任何其他资源(如libs),我会这样做:

代码语言:javascript
复制
#include <cctype>
#include <sstream>

// s is our escaped output string
std::string s = "";
// loop through all characters
for(char c : your_string)
{
    // check if a given character is printable
    // the cast is necessary to avoid undefined behaviour
    if(isprint((unsigned char)c))
        s += c;
    else
    {
        std::stringstream stream;
        // if the character is not printable
        // we'll convert it to a hex string using a stringstream
        // note that since char is signed we have to cast it to unsigned first
        stream << std::hex << (unsigned int)(unsigned char)(c);
        std::string code = stream.str();
        s += std::string("\\x")+(code.size()<2?"0":"")+code;
        // alternatively for URL encodings:
        //s += std::string("%")+(code.size()<2?"0":"")+code;
    }
}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2417588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档