.NET 内部编码
.NET 应用程序的内部编码是什么?(例如字符串对象) 我可以定义我的应用程序应使用什么编码吗?如果我将 .net 字符串写入文件。字符串是什么编码?
//编辑
Dim test as String="Das ist ein Test" <---what Encoding has this String?
Dim reader as New IO.StreamReader(docPath, _
System.Text.Encoding.GetEncoding("shift-jis"))
test=reader.ReadToEnd() <---and now? What Encoding has this String?
谢谢!
what is the internal encoding from .NET applications?(for example string objects) Can I define what encoding my appliction should use? If i write a .net string to a file. What encoding has the string?
//edit
Dim test as String="Das ist ein Test" <---what Encoding has this String?
Dim reader as New IO.StreamReader(docPath, _
System.Text.Encoding.GetEncoding("shift-jis"))
test=reader.ReadToEnd() <---and now? What Encoding has this String?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
UTF-16
仍然是 UTF-16。
StreamReader
类查看docPath
中的字节,并根据 shift-jis 编码将它们转换为 UTF-16。UTF-16
Still UTF-16. The
StreamReader
class looks at the bytes indocPath
and converts them to UTF-16 based on the shift-jis encoding.System.String
是 UTF-16。您可以使用的衍生物将其转换为各种其他编码System.Text.Encoding
类。回应编辑:
System.IO.StreamReader
,据我所知,如果未指定编码,则会尝试“猜测”正确的编码。System.IO.StreamWriter
写入为 UTF-8、IIRC。我对这些课程不太熟悉,因此请自行承担这些信息的风险;)System.String
is UTF-16. You can convert that to various other encodings using derivatives of theSystem.Text.Encoding
class.In response to edit:
System.IO.StreamReader
, as far as I am aware tries to "guess" as to the correct encoding if one is not specified.System.IO.StreamWriter
writes as UTF-8, IIRC. I'm less familiar with these classes so take that information at your own risk ;)正如所有其他答案一样:是的,2 字节 Unicode (UTF-16)。是的,您可以控制它如何写入光盘,就像 @Billy ONeal 所描述的那样。
关于您的问题是否可以控制这一点:不,这是不可能的。 .NET 内部始终在 Unicode UTF-16 上运行。没有这方面的设置。
As all of the other answers: yes, 2 byte Unicode (UTF-16). And yes, you can control how it writes to disc, like described by @Billy ONeal.
Concerning your question whether it is possible to control this: No, this is not possible. .NET will always run on Unicode UTF-16 internally. There are no settings for this.
.NET 内部使用 Unicode --UPDATED-- UTF-16。
但是,如果将字符串写入文件,则必须提供编码。如果您不这样做,.NET 将为您选择一种编码 - 通常是 UTF8。
这是反射的 File.WriteAllText:
Internally .NET uses Unicode --UPDATED-- UTF-16.
However, if you write the string to a file, you have to provide an encoding. If you don't .NET will choose an encoding for you - this is usually UTF8.
Here is reflectored File.WriteAllText: