【问题标题】:undo escape sequence "\" before unicode char在 unicode char 之前撤消转义序列“\”
【发布时间】:2014-02-28 10:36:40
【问题描述】:

我使用控制台程序(cmd 调用)将标准输入中的字符串转换为从标准输出接收到的特殊 Unicode 字符字符串。 C# 中的返回字符串转义了 Unicode 字符之前的转义反斜杠。

如何撤消这种转义?

示例返回字符串 =

stdout = "\\x284b\\x2817\\x2801\\x281d\\x2835 \\x281a\\x2801\\x281b\\x281e \\x280a\\x280d \\x2805\\x2815\\x280d\\x280f\\x2807\\x2811\\x281e\\x281e \\x2827\\x2811\\x2817\\x283a\\x2801\\x2813\\x2817\\x2807\\x2815\\x280e\\x281e\\x2811\\x281d \\x285e\\x2801\\x282d"

...但它应该是

stdout = "\x284b\x2817\x2801\x281d\x2835 \x281a\x2801\x281b\x281e \x280a\x280d \x2805\x2815\x280d\x280f\x2807\x2811\x281e\x281e \x2827\x2811\x2817\x283a\x2801\x2813\x2817\x2807\x2815\x280e\x281e\x2811\x281d \x285e\x2801\x282d"

我试图通过这样做来解决这个问题

var stdout2 = stdout.Replace(@"\\", @"\");

没有效果。

感谢 4 位帮助。

【问题讨论】:

  • 您如何查看字符串?你在调试器中暂停了吗?
  • 它没有任何效果,因为这些双反斜杠实际上并不存在。调试器试图提供太多帮助,以您在 C# 代码中编写的方式向您显示字符串。使用文本可视化器查看字符串 really 的样子。点击望远镜图标。
  • 于是我用调试器查看了这个结果。但调试输出:System.Diagnostics.Debug.WriteLine(stdout); 结果为:\x284b\x2817\x2801\x281d\x2835 ... 但它应该显示盲文字符,如 ⡋⠗⠁⠝⠵ ⠚⠁⠛⠞ ⠊⠍ ⠅⠕ ⠍⠏⠇⠑⠞⠞ ⠧⠑⠗⠺⠁⠓⠗⠇⠕⠎⠞⠑⠝ ⡞⠁⠭
  • 你得到答案了吗?
  • 这个\xNNNN 字符串是从哪里来的?我不知道任何使用反斜杠-x 来表示 UTF-16 代码单元的字符串转义格式。许多格式(JSON、C# 源代码等)使用反斜杠-u。

标签: c# string unicode char wchar


【解决方案1】:

你需要做的

stdout = stdout.Replace(@"\\", @"\");

改为。

【讨论】:

  • 好的,我知道我必须写得更清楚。是的,这就是我所做的,它没有效果
【解决方案2】:

我假设您不想删除字符串中的\\。它应该打印为\\x284b...。如果是这种情况,则使用@ 附加字符串。以下代码将打印\\

       string stdout = @"\\x284b\\x2817\\x2801\\x281d\\x2835 \\x281a\\x2801\\x281b\\x281e
       \\x280a\\x280d \\x2805\\x2815\\x280d\\x280f\\x2807\\x2811\\x281e\\x281e   
       \\x2827\\x2811\\x2817\\x283a\\x2801\\x2813\\x2817\\x2807\\x2815\\x280e\\x281e\\x2811
        \\x281d \\x285e\\x2801\\x282d";

        Console.Write(stdout);
        Console.Read();

【讨论】:

  • 并非如此。控制台 prog 将类似“\x284b”的字符串写入标准输出。我阅读了 C# 中的标准输出,并希望从字符串中获取 Unicode 字符“⡋”。但是 C# 转义了“\”并且不识别这是一个 Unicode 代码。如何将此字符串作为 Unicode 符号字符串?
  • 我在 VS 2010 和 C# 中进行了测试。它使用 \\.确保你已经@应用了
  • 我不明白为什么我必须使用@。我不想打印反斜杠和双反斜杠。最后,我想在我的系统输出或任何地方使用 ⡋⠗⠁⠝⠵ ⠚⠁⠛⠞ ⠊⠍ ⠅⠕⠍⠏⠇而不是 \x284b\x2817\x2801\x281d\x2835 \x281a\x2801\x281b\x281e需要。
  • 它在您的评论中显示为正方形。但是,如果你想删除 \\,只需使用 Console.Write(stdout.Replace("\\",""));
【解决方案3】:

结果来自名为 liblouis 的控制台程序

啊,好吧,LibLouis 有自己的奇特非标准字符串转义方案,记录在第 3 节 here 中。如果你想把它变成一个原始的未转义的 Unicode 字符串,除了\x 之外,你还需要处理许多反斜杠转义序列。类似的东西(未测试):

var escape = new Regex(@"\\(x[0-9A-Fa-f]{4}|y[0-9A-Fa-f]{5}|z[0-9A-Fa-f]{8}|.)");
var chars = new Dictionary<char, string> {
    { 'f', "\f" }, { 'n', "\n" }, { 'r', "\r" }, { 't', "\t" }, { 'v', "\v" },
    { 's', " " }, { 'e', "\x1B"}
};

var decoded_string = escape.Replace(encoded_string, match =>
    match.Length>2 ?
        Char.ConvertFromUtf32(
            int.Parse(
                match.Value.Substring(2),
                System.Globalization.NumberStyles.HexNumber
            )
        ) :
    chars.ContainsKey(match.Value[1]) ?
        chars[match.Value[1]] :
    match.Value.Substring(1)
);

【讨论】:

    【解决方案4】:

    最终,它既简单又有点复杂。我知道可以从integer 中创建char,从而找到了解决方案。因此,通过知道,样式 '\x284b' 的编码表示十六进制值 '284B',即十进制的 '10315',因此可以转换成char。所以我使用这些小函数将编码转换为Int32,然后将其转换为内部string……瞧

    /// <summary>
    /// Gets the char from unicode hexadecimal string.
    /// </summary>
    /// <param name="characterCode">The character code e.g. '\x2800'.</param>
    /// <returns>the current available unicode character if available e.g. ' '</returns>
    public static string GetCharFromUnicodeHex(String characterCode)
    {
    
        if (!String.IsNullOrEmpty(characterCode))
        {
            if (characterCode.StartsWith(@"\"))
            {
                characterCode = characterCode.Substring(1);
            }
            if (characterCode.StartsWith("x"))
            {
                characterCode = characterCode.Substring(1);
            }
    
            int number;
            bool success = Int32.TryParse(characterCode, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out number);
    
            if (success)
            {
                return GetCharFromUnicodeInt(number);
            }
        }
        return String.Empty;
    }
    
    
    /// <summary>
    /// try to parse a char from unicode int.
    /// </summary>
    /// <param name="number">The number code e.g. 10241.</param>
    /// <returns>the char of the given value e.g. ' '</returns>
    public static string GetCharFromUnicodeInt(int number)
    {
        try
        {
            char c2 = (char)number;
            return c2.ToString();
        }
        catch { }
        return String.Empty;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-31
      • 2011-05-15
      • 2021-12-26
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多