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 898979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:08:23+00:00 2026-05-15T15:08:23+00:00

How can I use LINQ to to XOR bytes together in an array? I’m

  • 0

How can I use LINQ to to XOR bytes together in an array? I’m taking the output of an MD5 hash and would like to XOR every four bytes together so that I can get a 32 bit int out of it. I could easily do this with a loop, but I thought it was an interesting problem for LINQ.

public static byte[] CompressBytes(byte[] data, int length)
{
    byte[] buffer = new byte[length];
    for (int i = 0; i < data.Length; i++)
    {
        for (int j = 0; j < length; j++)
        {
            if (i * length + j >= data.Length)
                break;
            buffer[j] ^= data[i * length + j];
        }
    }
    return buffer;
}

Slightly off topic, but is this even a good idea? If I need a int, would I be better off with a different hash function, or should I just take the first 4 bytes of the MD5 because XORing them all wouldn’t help any? Comments are welcome.

  • 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-15T15:08:24+00:00Added an answer on May 15, 2026 at 3:08 pm

    You can use the IEnumerable.Aggregate function (not actually LINQ, but most people refer to the LINQ-related extension methods as LINQ) to perform a custom aggregate. For example, you could compute the total XOR of a list of bytes like this:

    var xor = list.Aggregate((acc, val) => (byte)(acc ^ val));
    

    You can create a virtually unreadable chain of extension method calls to do what you’re after:

    int integer = BitConverter.ToInt32(Enumerable.Range(0, 3).
                  Select(i => data.Skip(i * 4).Take(4).
                      Aggregate((acc, val) => (byte)(acc ^ val))).ToArray(), 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using EF we can use LINQ to read data which is rather simple (especially
I am wondering if you can use Linq to SQL using a .mdf file
Is there an IBM Driver so we can use Linq to Entity framework to
Someone recently said that I can use Linq to SQL in my application even
Just wanted to know if I can use Linq on Javascript arrays. If not
i'm trying to figure out how I can use Linq to filter out some
I am trying to figure out if I can use LINQ to provide me
I'd like to use a DataGridViewRowCollection in a LINQ expression using extension methods and
I can do this by looping the result of a simplified query: var query

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.