【问题标题】:Replace non printable character with octal representation in C++/CLI在 C++/CLI 中用八进制表示替换不可打印字符
【发布时间】:2018-01-21 03:33:48
【问题描述】:

我需要使用 C++/CLI 将任何不可打印的字符替换为八进制表示。有一些使用 C# 的示例需要 lambda or linq。

// There may be a variety of non printable characters, not just the example ones.
String^ input = "\vThis has internal vertical quote and tab \t\v";
Regex.Replace(input,  @"\p{Cc}", ???  );

// desired output string = "\013This has internal vertical quote and tab \010\013"

这可以在 C++/CLI 中实现吗?

【问题讨论】:

    标签: regex c++-cli


    【解决方案1】:

    不确定是否可以内联。我用过这种逻辑。

    // tested
    String^ input = "\042This has \011 internal vertical quote and tab \042";
    String^ pat = "\\p{C}";
    String^ result = input;
    array<Byte>^ bytes;
    
    Regex^ search = gcnew Regex(pat);
    for (Match^ match = search->Match(input); match->Success; match = match->NextMatch()) {
        bytes = Encoding::ASCII->GetBytes(match->Value);
        int x = bytes[0];
    
        result = result->Replace(match->Value
            , "\\" + Convert::ToString(x,8)->PadLeft(3, '0'));
    }
    
    Console::WriteLine("{0} -> {1}", input, result);
    

    【讨论】:

      猜你喜欢
      • 2019-04-20
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      • 2017-08-30
      • 2018-04-04
      • 1970-01-01
      相关资源
      最近更新 更多