Encoding.GetEncoding(65005)出错

勿腻阳 2010-01-06 02:05:37
按照MSDN上的实例运行(见下):
using System;
using System.Text;

public class SamplesEncoding
{
public static void Main()
{
//// Get a UTF-32 encoding by codepage.
Encoding e1 = Encoding.GetEncoding(65005);

// Get a UTF-32 encoding by name.
Encoding e2 = Encoding.GetEncoding("utf-32");

// Check their equality.
Console.WriteLine("e1 equals e2? {0}", e1.Equals(e2));
Console.ReadLine();
}
}

但总是出错,错误如下:
没有可用于编码 65005 的数据……

在网上查了“utf-32”对应的代码页就是65005,可不知道为什么程序只要运行到“ Encoding e1 = Encoding.GetEncoding(65005);”就通过不了,请各位高手指点一二,谢谢!
...全文
223 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wartim 2010-01-06
  • 打赏
  • 举报
回复
可能是msdn没更新吧

http://blogs.msdn.com/michkap/archive/2007/08/11/4340636.aspx
Documentation does not always imply existence
You ever hear those fun stories about people who the fast food drive thru had to serve because someone left the outside sign's lights on?

Well, software doesn't really work that way.

So documentation does not always imply existence; sometimes, documentation just implies doc bugs!

Like the other day over in the microsoft.public.dotnet.internationalization when Bob Bins asked:

If you look at the MSDN sample for Encoding.GetEncoding Method (Int32) it shows the below sample code..

// Get a UTF-32 encoding by codepage.
Encoding e1 = Encoding.GetEncoding( 65005 );

// Get a UTF-32 encoding by name.
Encoding e2 = Encoding.GetEncoding( "utf-32" );

// Check their equality.
Console.WriteLine( "e1 equals e2? {0}", e1.Equals( e2 ) );


The problem is that the first line throws an exception:
System.NotSupportedException Additional information: No data is available for encoding 65005.

If I look at the code page property when using the "utf-32" string it says 12000.

Which one is correct? Why does the sample show 65005 as utf-32 when the function thinks 12000 is the utf32 codepage?

That is indeed the sample I found for the Encoding.GetEncoding Method (Int32), as Bob indicated.

Of course if you look at the UTF32Encoding class documentation it pretty clearly says what the the code page values are:

UTF32Encoding corresponds to the Windows code pages 12000 (little-endian byte order) and 12001 (big-endian byte order).

This is clearly a bit of a misnomer since no version of Windows that has ever shipped recognizes those code page values as being valid, which makes calling them Windows code pages a bit of an overstatement. I actually even mentioned them myself a couple of years ago back in Not every code page value is supported and the title alone makes it clear that not every code page value one might see is one that the operating system is going to recognize.

Now this problem seems pretty widespread in the docs (just search MSDN for that 65005 code page value to see what I mean -- there are enough instances of this problem to make it seem systemic!), but the most interesting one is in the Encoding.WindowsCodePage Property topic which has a huge list disguised as a code comment with several of these incorrect values. Understanding the source here might be a worthwhile exercise.

In any case, as MVP Mihai Nita pointed out, using the UTF32Encoding class is a much better idea for getting UTF-32 since that way both byte-endian-ness and BOM info can be more readily tailored....



勿腻阳 2010-01-06
  • 打赏
  • 举报
回复
可是MSDN上的例子就是这样写的呀,程序是按MSDN复制的,而且05版和08版的MSDN上的例子都是这样写的,难道是它上面的BUG?还是我的理解有错误?请大家说的再详细一些,谢谢!
xuzysun 2010-01-06
  • 打赏
  • 举报
回复
对头
angel6709 2010-01-06
  • 打赏
  • 举报
回复
foreach(EncodingInfo info in System.Text.Encoding.GetEncodings())
{
if (info.Name.Equals("utf-32"))
MessageBox.Show(info.CodePage.ToString());
}
CodePage==12000
qldsrx 2010-01-06
  • 打赏
  • 举报
回复
你在哪里查的,明显是错误的,“utf-32”对应的代码页应该是12000,怎么会是65005呢。
其实你直接使用Encoding.GetEncoding("utf-32"); 一样可以通过的

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧