默认的C#字符串编码 [英] Default C# String encoding

查看:311
本文介绍了默认的C#字符串编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在C#中的默认字符串编码的一些问题。我需要阅读从某些文件/数据包串。然而,这些字符串包括从128-256范围(扩展ASCII)字符,并且所有这些字符显示为问号,而不是正确的字符。例如,读取一个字符串时,可以拿出为S?meStr-N-如果字符串包含扩展ASCII字符。

I am having some issues with the default string encoding in C#. I need to read strings from certain files/packets. However, these strings include characters from the 128-256 range (extended ascii), and all of these characters show up as question marks , instead of the proper character. For example, when reading a string ,it could come up as "S?meStr?n?" if the string contained the extended ascii characters.

现在,有没有办法来改变默认编码为我的应用程序?我知道在Java中,你可以定义默认的字符命令行设置。

Now, is there any way to change the default encoding for my application? I know in java you could define the default character set from command line.

推荐答案

有没有一个单一的扩展ASCII的编码。有很多不同的8位编码它们与ASCII码为底128个值兼容的。

There's no one single "extended ASCII" encoding. There are lots of different 8-bit encodings which are compatible with ASCII for the bottom 128 values.

您需要找出编码文件的实际的使用,以及特定的阅读时的StreamReader (或者你使用任何其他)。例如,您可能希望编码的Windows 1252

You need to find out what encoding your files actually use, and specific that when reading the data with StreamReader (or whatever else you're using). For example, you may want encoding Windows-1252:

Encoding encoding = Encoding.GetEncoding(1252);



.NET字符串的总是的的UTF-16代码点序列。你不能改变的,你不应该尝试。 (在Java的也是如此,你真的不应的调用的getBytes()时所使用的平台默认的编码等,除非那是你真正,真正的意思。)

.NET strings are always sequences of UTF-16 code points. You can't change that, and you shouldn't try. (That's true in Java as well, and you really shouldn't use the platform default encoding when calling getBytes() etc unless that's what you really, really mean.)

这篇关于默认的C#字符串编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆