是否可以检测UDP响应编码?
问题:
我通过 UDP 查询 Quake3 主服务器,并得到如下响应。 正如你所看到的,我很难弄清楚服务器发送的内容的编码...... 有没有办法检测或设置接收编码?
baBuffer = new byte[1024*100]; // 100 kb should be enough
int recv = sctServerConnection.ReceiveFrom(baBuffer, ref tmpRemote);
Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
System.Text.Encoding encResponseEncoding = System.Text.Encoding.Default; // Wrong...
//encResponseEncoding = System.Text.Encoding.ASCII;
//encResponseEncoding = System.Text.Encoding.UTF8;
//encResponseEncoding = System.Text.Encoding.GetEncoding(437); // ANSI-DOS
//encResponseEncoding = System.Text.Encoding.GetEncoding(1252);// ANSI-WestEurope
//encResponseEncoding = System.Text.Encoding.GetEncoding(1250); // Ansi-Centraleuro
//encResponseEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
//encResponseEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
//encResponseEncoding = System.Text.Encoding.UTF32;
encResponseEncoding = System.Text.Encoding.UTF7; // Bingo !
Question:
I query a Quake3 masterserver via UDP, and get the response as below.
As you can see, I had trouble figuring out the encoding of what the server sent...
Is there any way to detect or set the receive encoding ?
baBuffer = new byte[1024*100]; // 100 kb should be enough
int recv = sctServerConnection.ReceiveFrom(baBuffer, ref tmpRemote);
Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
System.Text.Encoding encResponseEncoding = System.Text.Encoding.Default; // Wrong...
//encResponseEncoding = System.Text.Encoding.ASCII;
//encResponseEncoding = System.Text.Encoding.UTF8;
//encResponseEncoding = System.Text.Encoding.GetEncoding(437); // ANSI-DOS
//encResponseEncoding = System.Text.Encoding.GetEncoding(1252);// ANSI-WestEurope
//encResponseEncoding = System.Text.Encoding.GetEncoding(1250); // Ansi-Centraleuro
//encResponseEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
//encResponseEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
//encResponseEncoding = System.Text.Encoding.UTF32;
encResponseEncoding = System.Text.Encoding.UTF7; // Bingo !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编码(如果它实际上是文本)由协议决定。如果您没有协议规范并且没有源代码,那么,是的,您将不得不猜测。
The encoding (if it is actually text) is determined by the protocol. If you don't have a protocol spec and you don't have the source code then, yes, you'll have to guess.
没有办法安全地检测编码,你只能猜测它。另请参阅如何检测文本文件的编码/代码页。
There is no way to safely detect encoding, you can just guess it. See also How can I detect the encoding/codepage of a text file.
您可以查找字节顺序标记 (BOM)。这是我使用的一些 VB.Net 代码:
You can look for the Byte Order Mark (BOM). Here's some VB.Net code that I use: