是否可以检测UDP响应编码?

发布于 2024-08-18 06:21:24 字数 1117 浏览 2 评论 0原文

问题:

我通过 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 技术交流群。

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

发布评论

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

评论(3

醉生梦死 2024-08-25 06:21:24

编码(如果它实际上是文本)由协议决定。如果您没有协议规范并且没有源代码,那么,是的,您将不得不猜测。

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.

岛歌少女 2024-08-25 06:21:24

没有办法安全地检测编码,你只能猜测它。另请参阅如何检测文本文件的编码/代码页

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.

格子衫的從容 2024-08-25 06:21:24

您可以查找字节顺序标记 (BOM)。这是我使用的一些 VB.Net 代码:

Private Shared Function GetStringFromBytes(ByVal bytes() As Byte) As String
    Dim ByteLegth = bytes.Count
    If (ByteLegth >= 3) AndAlso (bytes(0) = &HEF) AndAlso (bytes(1) = &HBB) AndAlso (bytes(2) = &HBF) Then
        Return System.Text.Encoding.UTF8.GetString(bytes)
    ElseIf (ByteLegth >= 2) AndAlso (bytes(0) = &HFE) AndAlso (bytes(1) = &HFF) Then
        Return System.Text.Encoding.BigEndianUnicode.GetString(bytes)
    ElseIf (ByteLegth >= 2) AndAlso (bytes(0) = &HFF) AndAlso (bytes(1) = &HFE) Then
        Return System.Text.Encoding.Unicode.GetString(bytes)
    ElseIf (ByteLegth >= 2) AndAlso (bytes(0) = &H0) AndAlso (bytes(1) = &H0) AndAlso (bytes(2) = &HFE) AndAlso (bytes(3) = &HFF) Then
        Return New System.Text.UTF32Encoding(True, True).GetString(bytes)
    ElseIf (ByteLegth >= 2) AndAlso (bytes(0) = &HFF) AndAlso (bytes(1) = &HFE) AndAlso (bytes(2) = &H0) AndAlso (bytes(3) = &H0) Then
        Return System.Text.Encoding.UTF32.GetString(bytes)
    Else
        'No BOM, assume ASCII
        Return System.Text.Encoding.ASCII.GetString(bytes)
    End If
End Function

You can look for the Byte Order Mark (BOM). Here's some VB.Net code that I use:

Private Shared Function GetStringFromBytes(ByVal bytes() As Byte) As String
    Dim ByteLegth = bytes.Count
    If (ByteLegth >= 3) AndAlso (bytes(0) = &HEF) AndAlso (bytes(1) = &HBB) AndAlso (bytes(2) = &HBF) Then
        Return System.Text.Encoding.UTF8.GetString(bytes)
    ElseIf (ByteLegth >= 2) AndAlso (bytes(0) = &HFE) AndAlso (bytes(1) = &HFF) Then
        Return System.Text.Encoding.BigEndianUnicode.GetString(bytes)
    ElseIf (ByteLegth >= 2) AndAlso (bytes(0) = &HFF) AndAlso (bytes(1) = &HFE) Then
        Return System.Text.Encoding.Unicode.GetString(bytes)
    ElseIf (ByteLegth >= 2) AndAlso (bytes(0) = &H0) AndAlso (bytes(1) = &H0) AndAlso (bytes(2) = &HFE) AndAlso (bytes(3) = &HFF) Then
        Return New System.Text.UTF32Encoding(True, True).GetString(bytes)
    ElseIf (ByteLegth >= 2) AndAlso (bytes(0) = &HFF) AndAlso (bytes(1) = &HFE) AndAlso (bytes(2) = &H0) AndAlso (bytes(3) = &H0) Then
        Return System.Text.Encoding.UTF32.GetString(bytes)
    Else
        'No BOM, assume ASCII
        Return System.Text.Encoding.ASCII.GetString(bytes)
    End If
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文