Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »

Review Suggested Edit

You can't approve or reject suggested edits because you haven't yet earned the Edit Posts ability.

Approved.
This suggested edit was approved and applied to the post about 3 years ago by celtschk‭.

0 / 255
Run-length encode a byte sequence
  • Run-length encoding is a simple compression technique which compresses sequences of repeating identical bytes.
  • The encoding rules for this task are as follows:
  • Any sequence of n = 3 to 63 identical bytes is replaced by a byte with value (n+0xc0) followed by only one copy of that byte. If any of the original bytes has value 0xc0 or above, such encoding also happens if that byte occurs only one or two times in a row (thus the encoded sequence may actually be longer if such bytes occur).
  • The task is to write an RLE encoder. The input is a sequence of byte values. It may be a sequence of actual bytes, or a sequence of numbers in the range 0 to 255, whatever is more convenient. The output is again a sequence of byte values (again, in any suitable form), which is the proper run-length encoding of the input sequence.
  • Examples (byte values are given in 2-digit hex):
  • ```
  • 00 01 02 03 04 → 00 01 02 03 04
  • 00 00 00 01 01 00 → c3 00 01 01 00
  • be bf c0 c1 c2 → be bf c1 c0 c1 c1 c1 c2
  • 00 00 00 … (63 bytes) → ff 00
  • 00 00 00 … (64 bytes) → ff 00 00
  • 00 00 00 … (65 bytes) → ff 00 00 00
  • 00 00 00 … (66 bytes) → ff 00 c3 00
  • ff ff ff … (63 bytes) → ff ff
  • ff ff ff … (64 bytes) → ff ff c1 ff
  • ff ff ff … (65 bytes) → ff ff c2 ff
  • ff ff ff … (66 bytes) → ff ff c3 ff
  • ```
  • This is a <span class="badge is-tag">code-golf</span> challenge; the shortest program for a language wins.
  • Run-length encoding is a simple compression technique which compresses sequences of repeating identical bytes.
  • The encoding rules for this task are as follows:
  • Any sequence of $n$ identical bytes ($3 \leq n \leq 63$) is replaced by a byte with value `n+0xc0` followed by only one copy of that byte. If any of the original bytes has value `0xc0` or above, such encoding also happens if that byte occurs only one or two times in a row (thus the encoded sequence may actually be longer if such bytes occur).
  • The task is to write a run-length encoder. The input is a sequence of bytes. It may be a sequence of actual bytes, or a sequence of numbers in the range 0 to 255 (inclusive), whichever is more convenient. The output is again a sequence of bytes (again, in any suitable form), which is the proper run-length encoding of the input sequence.
  • Examples (byte values are given in 2-digit hex):
  • ```
  • 00 01 02 03 04 → 00 01 02 03 04
  • 00 00 00 01 01 00 → c3 00 01 01 00
  • be bf c0 c1 c2 → be bf c1 c0 c1 c1 c1 c2
  • 00 00 00 … (63 bytes) → ff 00
  • 00 00 00 … (64 bytes) → ff 00 00
  • 00 00 00 … (65 bytes) → ff 00 00 00
  • 00 00 00 … (66 bytes) → ff 00 c3 00
  • ff ff ff … (63 bytes) → ff ff
  • ff ff ff … (64 bytes) → ff ff c1 ff
  • ff ff ff … (65 bytes) → ff ff c2 ff
  • ff ff ff … (66 bytes) → ff ff c3 ff
  • ```
  • This is a <span class="badge is-tag">code-golf</span> challenge; the shortest program for a language wins.

Suggested about 3 years ago by user‭