UtF-8提供额外的德语字符串

本文关键字:德语 字符串 UtF-8 | 更新日期: 2023-09-27 18:29:55

我有一个文件名testtäüßÖÜ。我想使用c#转换成UTF-8。

string test ="testtäöüß";
var bytes = new List<byte>(test.Length);
        foreach (var c in test)
            bytes.Add((byte)c);
        var retValue = Encoding.UTF8.GetString(bytes.ToArray());

运行完这段代码后,我的输出是:‘testt mit Umlauteäü?x.其中mit元音变音符是额外的文本

有人能帮我吗?

提前谢谢。

UtF-8提供额外的德语字符串

你不能那样做。不能将UTF-8字符强制转换为字节。UTF-8对于ASCII以外的任何东西都需要至少两个字节,字节无法存储此

使用而不是创建列表

byte[] bytes = System.Text.Encoding.UTF8.GetBytes (test);

我认为,Tseng的意思是以下

取自:http://www.chilkatsoft.com/p/p_320.asp

        System.Text.Encoding utf_8 = System.Text.Encoding.UTF8;
        // This is our Unicode string:
        string s_unicode = "abcéabc";
        // Convert a string to utf-8 bytes.
        byte[] utf8Bytes = System.Text.Encoding.UTF8.GetBytes(s_unicode);
        // Convert utf-8 bytes to a string.
        string s_unicode2 = System.Text.Encoding.UTF8.GetString(utf8Bytes);
        MessageBox.Show(s_unicode2);