从转义的ASCII序列中读取UTF8 / UNICODE字符

我在文件中有以下名称,我需要将字符串作为UTF8编码的字符串读取,所以从这里:

test_\303\246\303\270\303\245.txt 

我需要获得以下内容:

 test_æøå.txt 

你知道如何使用C#实现这一目标吗?

假设你有这个字符串:

 string input = "test_\\303\\246\\303\\270\\303\\245.txt"; 

IE字面意思

 test_\303\246\303\270\303\245.txt 

你可以这样做:

 string input = "test_\\303\\246\\303\\270\\303\\245.txt"; Encoding iso88591 = Encoding.GetEncoding(28591); //See note at the end of answer Encoding utf8 = Encoding.UTF8; //Turn the octal escape sequences into characters having codepoints 0-255 //this results in a "binary string" string binaryString = Regex.Replace(input, @"\\(?[0-7]{3})", delegate(Match m) { String oct = m.Groups["num"].ToString(); return Char.ConvertFromUtf32(Convert.ToInt32(oct, 8)); }); //Turn the "binary string" into bytes byte[] raw = iso88591.GetBytes(binaryString); //Read the bytes into C# string string output = utf8.GetString(raw); Console.WriteLine(output); //test_æøå.txt 

“二进制字符串”,我的意思是一个字符串,只包含代码点0-255的字符。 因此它相当于一个穷人的byte[] ,你在索引i处检索字符的代码点,而不是在索引ibyte[]中的byte值(这是我们几年前在javascript中所做的)。 因为iso-8859-1将前256个unicode代码点精确映射到单个字节,所以它非常适合将“二进制字符串”转换为byte[]