简体   繁体   English

C#ASCII或Unicode

[英]C# ASCII or Unicode

hi im a beginner in programming and network development. 您是编程和网络开发的初学者。 i have a question regarding ASCII and Unicode encoding. 我有一个关于ASCII和Unicode编码的问题。

in msdn and other web examples do the following: 在msdn和其他Web示例中执行以下操作:

byte[] byteData = Encoding.ASCII.GetBytes(data);

is this because these code samples are old? 这是因为这些代码示例是旧的吗? shouldn't it be: 不应该是:

byte[] byteData = Encoding.Unicode.GetBytes(data);

thanks for your input! 感谢您的输入!

It depends - do you want the result to be in ASCII or UTF-16? 这取决于 - 您希望结果是ASCII还是UTF-16? Each is wrong when you want the other. 当你想要另一个时,每个人都错了。

If you're talking some network protocol, you must find out which character encoding is expected by the protocol. 如果您正在谈论某些网络协议,则必须找出协议所期望的字符编码。 Use the wrong encoding, and Bad Things Will Happen. 使用错误的编码,坏事将发生。

Of course ASCII has massive restrictions - it's very English-based (Latin characters only, no accents) but it's correct for some protocols. 当然,ASCII有很大的限制 - 它是基于英语的(仅限拉丁字符,没有重音符号),但它对某些协议是正确的。 Others may use UTF-16 (Encoding.Unicode), UTF-8 or other encodings... or they'll let you specify the encoding yourself within the protocol. 其他人可能会使用UTF-16(Encoding.Unicode),UTF-8或其他编码...或者他们会让你自己在协议中指定编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM