【问题标题】:Convert Unicode string into proper string将 Unicode 字符串转换为正确的字符串
【发布时间】:2015-05-18 06:33:53
【问题描述】:

我有一个包含 unicode 数据的字符串。

我想把它写在一个文件中。当数据写入文件时,它给了我简单的 unicode 值而不是英语以外的语言。

string originalString = ((char)(buffer[index])).ToString();
//sb.Append(DecodeEncodedNonAsciiCharacters(originalString.ToString()));
foreach (char c1 in originalString)
{
    // test if char is ascii, otherwise convert to Unicode Code Point
    int cint = Convert.ToInt32(c1);
    if (cint <= 127 && cint >= 0)
        asAscii.Append(c1.ToString());
    else
    {
        //String s = Char.ConvertFromUtf32(cint);
        asAscii.Append(String.Format("\\u{0:x4} ", cint).Trim());
       // asAscii.Append(s);
    }
}

sb.Append((asAscii));
Console.WriteLine();

当我看到输出文件时,数据显示如下

1 00:00:27,709-->00:00:32,959 1.2 \u00e0\u00a4\u0085\u00e0\u00a4\u00b0\u00e0\u00a4\u00ac \u00e0\u00a4\u00b2\u00e0\u00a5\u008b\u00e0\u00a4\u0097 28 \u00e0\u00a4\u00b0\u00e0\u00a4\u00be\u00e0\u00a4\u009c\u00e0\u00a5\u008d\u00e0\u00a4\u00af \u00e0\u00a4\u0094\u00e0\u00a4\u00b0 \u00e0\u00a4\u00b8\u00e0\u00a4\u00be\u00e0\u00a4\u00a4 \u00e0\u00a4\u0095\u00e0\u00a5\u0087\u00e0\u00a4\u0082\u00e0\u00a4\u00a6\u00e0\u00a5\u008d\u00e0\u00a4\u00b0 \u00e0\u00a4\u00b6\u00e0\u00a4\u00be\u00e0\u00a4\u00b8\u00e0\u00a4\u00bf\u00e0\u00a4\u00a4 \u00e0\u00a4\u00aa\u00e0\u00a5\u008d\u00e0\u00a4\u00b0\u00e0\u00a4\u00a6\u00e0\u00a5\u0087\u00e0\u00a4\u00b6

但它应该看起来像这样

1 00:00:27,400 --> 00:00:32,760 1.2 अरब लोग 28 राज्य और सात केंद्र शासित प्रदेश

我尝试了很多事情,但都没有完成我的工作。

【问题讨论】:

  • Unicode 是对字符串的正确编码。只是说...
  • @PradnyaBolli:链接到谷歌被认为是“没有建设性的”。
  • 读取字符串的代码错误,必须修复。读取的代码使用了错误的编码。流的默认编码是 ASCII,在这种情况下使用必须指定 UNICODE 编码。
  • 这些技巧都不适合我。但我非常感谢您的快速回复。我仍在寻找解决方案

标签: c# unicode


【解决方案1】:
string unicodeString = "This string contains the unicode character Pi(\u03a0)";

     // Create two different encodings.
     Encoding ascii = Encoding.ASCII;
     Encoding unicode = Encoding.Unicode;

     // Convert the string into a byte[].
     byte[] unicodeBytes = unicode.GetBytes(unicodeString);

     // Perform the conversion from one encoding to the other.
     byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);

     // Convert the new byte[] into a char[] and then into a string.
     // This is a slightly different approach to converting to illustrate
     // the use of GetCharCount/GetChars.
     char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
     ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
     string asciiString = new string(asciiChars);

     // Display the strings created before and after the conversion.
     Console.WriteLine("Original string: {0}", unicodeString);
     Console.WriteLine("Ascii converted string: {0}", asciiString);

【讨论】:

  • 感谢您的答复,但我已经尝试过,结果是..1 00:00:27,709-> 00:00:00:00:00:32,959 1.2 1.2 1.2ििब28oldy 28 oldyलॿि >
  • ,但应该像这个1 00:00:27,400-> 00:00:00:00:32,760 1.2अलोग28
  • 它用于 UTF8 解码,您可能需要将其应用于用于将其编码为 un​​icode 的解码
  • 大家好,我被这个问题困住了。当我按原样保存缓冲区时,数据会正确显示,但我需要从缓冲区中提取数据并将其保存到字符串中。我认为在将字节数组转换为正确编码的字符串时可能会丢失数据
  • 非常感谢。我通过替换一行得到了解决方案.... Encoding ascii = Encoding.ASCII; to Encoding utf = Encoding.UTF8;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-04
  • 2011-06-09
  • 2010-12-09
相关资源
最近更新 更多