Question

I'm trying to make a loop that will recurse through an array of bytes and compare them to the next one in the array (presumably using a for loop to iterate through each entry). If the two are the same I need it to increment an int variable and then continue. If the two are not the same it needs to add the int variable to a list followed by its own byte value and then it needs "adopt" this new value as its own and start the whole thing over again until the end of the array. Its a very weird thing but it needs to be done this way so I can write the int/byte pairs to a file in the right order and with the right values.

The concern here is to get the number of consecutive entries that are the same, make note of it, then move on to the next value and repeat. If for example value 3 that we encounter is the same as value 1 that's of no concern to us. As long as we get the number of consecutive entries for value 3 we've done our job.

A couple other possibly helpful points.

  • The byte values could in this case span the full range 0-255.
  • The array size may be up to 150 million byte entries so efficiency is important.
  • The array size is accessible beforehand.
  • Lastly the byte array is a byte variable in a Struct.

I hope this make sense. Thanks in advance.

EDIT: Sorry if I wasn't quite clear before and maybe I should re-title the question as well.

To clarify, I understand what I need to do here just not how to go about it. So the question I guess would be how do I loop through this comparison and then swap out what I'm comparing when I get a false return. Most importantly how do I do that when what I'm comparing could have 255 values and I'm not aware of them. I can't really picture how to code this so I just keep sitting there staring at VS :)

Does that make better sense ? If not I apologize :)

EDIT 2: Here's the end result I came up with if anyone cares to look at it. It was inspired by aligray's code down below.

            int count = 0;
            byte previous = tiles[0].TileTypeId;
            List<int> typeCount = new List<int>();
            List<byte> type = new List<byte>();
            for (int i = 0; i < worldSize; i++)
            {
                byte current = tiles[i].TileTypeId;
                if (previous == current)
                {
                    count++;
                }
                else
                {
                    typeCount.Add(count);
                    type.Add(previous);
                    previous = current;
                    count = 1;
                }
            } 
Was it helpful?

Solution

If I understood the question correctly, hopefully this will get you started:

int count = 0;
byte previous = byteArray[0];
List<int> list = new List<int>();

for (int i = 1; i < byteArray.Length; i++)
{
    byte current = byteArray[i];
    if (previous == current)
    {
        count++;
    }
    else
    {
        list.Add(count);
        list.Add(Convert.ToInt32(current));
    }

    previous = current;
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top