.NET 内部编码

发布于 2024-10-01 17:57:14 字数 396 浏览 2 评论 0原文

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风吹雨成花 2024-10-08 17:57:14
Dim test as String="Das ist ein Test" <---what Encoding has this String?

UTF-16

Dim reader as New IO.StreamReader(docPath,
    System.Text.Encoding.GetEncoding("shift-jis"))
test=reader.ReadToEnd <---and now? What Encoding has this String?

仍然是 UTF-16。 StreamReader 类查看 docPath 中的字节,并根据 shift-jis 编码将它们转换为 UTF-16。

Dim test as String="Das ist ein Test" <---what Encoding has this String?

UTF-16

Dim reader as New IO.StreamReader(docPath,
    System.Text.Encoding.GetEncoding("shift-jis"))
test=reader.ReadToEnd <---and now? What Encoding has this String?

Still UTF-16. The StreamReader class looks at the bytes in docPath and converts them to UTF-16 based on the shift-jis encoding.

帅冕 2024-10-08 17:57:14

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 the System.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 ;)

倒数 2024-10-08 17:57:14

正如所有其他答案一样:是的,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.

滥情稳全场 2024-10-08 17:57:14

.NET 内部使用 Unicode --UPDATED-- UTF-16。

但是,如果将字符串写入文件,则必须提供编码。如果您不这样做,.NET 将为您选择一种编码 - 通常是 UTF8
这是反射的 File.WriteAllText:

public static void WriteAllText(string path, string contents)
{
    if (path == null)
    {
        throw new ArgumentNullException("path");
    }
    if (path.Length == 0)
    {
        throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
    }
    InternalWriteAllText(path, contents, StreamWriter.UTF8NoBOM);
}

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:

public static void WriteAllText(string path, string contents)
{
    if (path == null)
    {
        throw new ArgumentNullException("path");
    }
    if (path.Length == 0)
    {
        throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
    }
    InternalWriteAllText(path, contents, StreamWriter.UTF8NoBOM);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文