Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6209127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:55:43+00:00 2026-05-24T05:55:43+00:00

I need to compare the first 5 bytes of a UDP packet to two

  • 0

I need to compare the first 5 bytes of a UDP packet to two definitions, and act on them accordingly if it matches one. However, how should I represent the Hex for this since this obviously won’t/isn’t working?

The commented lines are the original way I was doing it, but not very efficient.

private static void HandleQuery(Socket socket, EndPoint Remote)
{
    byte[] A2S_INFO_REQUEST = StringToByteArray("\xFF\xFF\xFF\xFF\x54");
    byte[] A2S_PLAYER = StringToByteArray("\xFF\xFF\xFF\xFF\x55");

    byte[] data = new byte[1400];
    int recv = socket.ReceiveFrom(data, ref Remote);

    // A2S_INFO QUERY
    //if (recv == 25 && data[4] == 84)
    if (CompareByteArray(A2S_INFO_REQUEST, data, 5))
    {
        ReplyInfoQuery(socket, Remote);
    }

    // A2S_PLAYER QUERY
    //if (recv == 9)
    if (CompareByteArray(A2S_PLAYER, data, 5))
    {
        ReplyPlayerQuery(socket, Remote);
    }
}

private static byte[] StringToByteArray(String hex)
{
    int NumberChars = hex.Length;
    byte[] bytes = new byte[NumberChars / 2];
    for (int i = 0; i < NumberChars; i += 2)
    bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
    return bytes;
}

private static bool CompareByteArray(byte[] a1, byte[] a2, int len)
{
    for (int i = 0; i < len; i++)
    {
        if (a1[i] != a2[i])
        {
            return false;
        }
    }

    return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:55:45+00:00Added an answer on May 24, 2026 at 5:55 am

    You can fill a byte array easier:

    //byte[] A2S_PLAYER = StringToByteArray("\xFF\xFF\xFF\xFF\x55");
    byte[] A2S_PLAYER = new byte[] {0xFF, 0xFF, 0xFF, 0xFF, 0x55} ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to compare the content of two tables, more exactly two columns (one
I have two tables that I need to compare and update values in one
I have two binary files with different sizes. I need to compare first N
First of all, I don't need a textual comparison so Beyond Compare doesn't do
I need to compare the integer part of two doubles for inequality and I'm
I need to compare dozens of fields in two objects (instances of the same
I need to compare two Date s (e.g. date1 and date2 ) and come
I would like to know how to compare two two-dimension arrays value. First array
In .NET, I need a way to compare two files. I thought of a
I have run into a situation where I need to compare two different lists

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.