calculate numbers in array (run length decoding)

170 Views Asked by At
        int[] bytes = new int[9] { 123, 5, 65, 123, 6, 77, 123, 4, 101 };       
        int count = 1;          
        for (int i = 1; i < bytes.Length; i++)
        {
            
            if (bytes[i] == 123)
            {
                count++;              
            }
            else
            {
                Console.WriteLine(bytes[i]);
            }
        }
        Console.ReadLine();

Im a beginner in programming. The 123 is some type of "marker"
I dont know how to do a console output like this: 65 65 65 65 65 77 77 77 77 77 77 101 101 101 101
I would appreciate any help

1

There are 1 best solutions below

3
On BEST ANSWER

To decode anything, you really need a specification of what that something is. In this case, I could speculate that:

  • 123 means "the following two bytes X and Y should be interpreted as X instances (0-255) of the payload Y"

so you would parse one byte, if it isn't 123: give up because you don't have any other rules to follow, but otherwise, read X and Y, and output X copies of the value Y

In pseudo-code:

while(TryTakeByte(out val)))
    switch val
        123:
            if (!TryTakeByte(out x) || !TryTakeByte(out y)) FailEndOfStream()
            for (i from 0 to x)
                write y
        default:
            FailNoClueWhatToDo() // refer to protocol specification