在 C++/CLI 中用八进制表示替换不可打印字符

Replace non printable character with octal representation in C++/CLI

我需要使用 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 = "3This has internal vertical quote and tab 03"

这在 C++/CLI 中可行吗?

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

// tested
String^ input = "2This has 1 internal vertical quote and tab 2";
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);