1

我有文件名 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 Umlaute 是额外的文本。

有谁能够帮助我 ?

提前致谢。

4

2 回答 2

2

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

而不是创建一个列表,使用

byte[] bytes = System.Text.Encoding.UTF8.GetBytes (test);
于 2013-09-02T05:27:29.167 回答
1

我想,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);
于 2013-09-02T08:54:50.703 回答