-2
        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

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
powerYY
  • 3
  • 1
  • 1
    "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" Honestly, me neither. Is this some type of [run-length-encoding](https://en.wikipedia.org/wiki/Run-length_encoding)? Anyway what exactly is your problem? What works and what not? What is the result you actually get from the above code? – MakePeaceGreatAgain Dec 14 '20 at 08:55
  • If it's run-length encoding, surely you need to first get the number of times you want it repeated, and then next get the byte to repeat? These are different elements, so the first iteration will give you how many you want, and then the second will give you the byte to repeat. You'll then need a loop to repeat that byte that number of times. – ProgrammingLlama Dec 14 '20 at 08:57

1 Answers1

3

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
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • "In this case, I could speculate that:" Should we really do that? – MakePeaceGreatAgain Dec 14 '20 at 09:21
  • @HimBromBeere as long as we call out that it is speculation, and make note that we should really be checking what the specification says: sure – Marc Gravell Dec 14 '20 at 09:29
  • "if it isn't 123: give up because you don't have any other rules to follow" - Well, the standard for flag-based RLE is that anything encountered that does not match the flag pattern can be considered uncompressed, and should simply be copied. I mean, otherwise, there's no point in even _having_ the bytes 123; if all expected data are length-value pairs you wouldn't _need_ any markers. – Nyerguds Feb 09 '21 at 12:26