# how to encode exotic text byte/hex pattern/sequence

**URL:** <https://discuss.python.org/t/how-to-encode-exotic-text-byte-hex-pattern-sequence/28575>\
**Category:** Python Help\
**Created:** [June 27, 2023, 12:42pm UTC](https://discuss.python.org/t/how-to-encode-exotic-text-byte-hex-pattern-sequence/28575 "2023-06-27T12:42:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![supercool3333](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@supercool3333](https://discuss.python.org/u/supercool3333)\
**Post date:** [June 27, 2023, 12:42pm UTC](https://discuss.python.org/t/how-to-encode-exotic-text-byte-hex-pattern-sequence/28575/1 "2023-06-27T12:42:53Z")

</div>

Want to encode some raw text byte/hex pattern/sequences with GH,Python or C#. Goal: 8bit Grayscale images.  
kind of run length structure: 4 or 1 byte chunks but you have to extract chunksize inside every chunk.  
So does anybody have fast code to compare every first byte pair of a chunk, read the bytes (4 or one)  
and then get the startindex/offset of the next chunk. iterate and maybe draw the decompressed pixels. chatgpt?

 ![example_explain](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/7/73602962969fedbc0d00eac5334ead641e7daf1e.png)

---

<div class="post-metadata">

**Author:** ![supercool3333](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@supercool3333](https://discuss.python.org/u/supercool3333)\
**Post date:** [July 3, 2023, 7:35am UTC](https://discuss.python.org/t/how-to-encode-exotic-text-byte-hex-pattern-sequence/28575/2 "2023-07-03T07:35:32Z")

</div>

> [@supercool3333](#):
>
> Want to encode some raw text

Sorry, want to DECODE the text files.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [July 3, 2023, 3:03pm UTC](https://discuss.python.org/t/how-to-encode-exotic-text-byte-hex-pattern-sequence/28575/3 "2023-07-03T15:03:56Z")

</div>

I can’t understand the rule for the decoding from this diagram. Step by step, how does the reasoning work? In particular, what is the rule that tells us, whether we have a 1-byte or a 4-byte chunk? When it is a 1-byte chunk, do we just output that value directly, or just what? And then - where exactly are you stuck? Do you know how to read from a file? If you have the output values, do you know how to create an image file? Why is it important for the code to be fast? (For example, did you already write something that is too slow?)

Also: how will you decide the width of the output image?

---

<div class="post-metadata">

**Author:** ![supercool3333](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@supercool3333](https://discuss.python.org/u/supercool3333)\
**Post date:** [July 7, 2023, 7:43am UTC](https://discuss.python.org/t/how-to-encode-exotic-text-byte-hex-pattern-sequence/28575/4 "2023-07-07T07:43:52Z")

</div>

Rule: read the first and second byte, compare them, if equal then its a 4 byte chunk. If they are not equal take the first value directly (1byte chunk).

I have written something inside Rhino3d/Grasshopper3d. i have read/convered the text with Python and iterate with a small plugin (Hoopsnake/ Loop) — too slow.

The example files are fragments of larger textfiles containing 2d/3dcoordinates/surfaces creases/colors/jpg-images and **these encoded grayscale Alpha Channels**. The width/height informations are readable inside this file.

Stuck: dont know nothin how to struct/partiton/indexing-chunks in python. (itertools?memview?buffer?yield?partial or complete file)

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [July 7, 2023, 6:06pm UTC](https://discuss.python.org/t/how-to-encode-exotic-text-byte-hex-pattern-sequence/28575/5 "2023-07-07T18:06:03Z")

</div>

Ah, so then the reason for adding 2 is that the next two bytes in a 4-byte chunk are a repetition count, and a single byte value would be used for a single repetition. So we win when there is a long repeated run, lose for exactly two or three consecutive appearances of a value, and otherwise don’t change anything. Ok.

> [@supercool3333](#):
>
> i have read/convered the text with Python and iterate with a small plugin (Hoopsnake/ Loop) — too slow.

What exactly does this mean? What “text” are you reading and converting, and how exactly is the file represented? (I.e.: if you try to open the file with a text editor, do you see a hex dump, some incomprehensible garbage, or just what?) What are Hoopsnake and Loop; how are you making a “plugin” with them; and why is that helpful for “iterating” (as opposed to just writing a normal loop in Python code)? What actual overall code do you have? How much time does it take, and how far is that from your requirement?

---

<div class="post-metadata">

**Author:** ![supercool3333](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@supercool3333](https://discuss.python.org/u/supercool3333)\
**Post date:** [July 11, 2023, 1:30am UTC](https://discuss.python.org/t/how-to-encode-exotic-text-byte-hex-pattern-sequence/28575/6 "2023-07-11T01:30:11Z")

</div>

> **[Encode\_iterate\_split\_ text byte/hex pattern/sequence \_hoopsnake](https://discourse.mcneel.com/t/encode-iterate-split-text-byte-hex-pattern-sequence-hoopsnake/162067)**
>
> Want to encode some raw text byte/hex pattern/sequences with GH,Python or C#. Goal: 8bit Grayscale images. kind of run length structure: 4 or 1 byte chunks but you have to extract chunksize inside every chunk. So does anybody have fast python...

> **[Hoopsnake](https://www.food4rhino.com/en/app/hoopsnake)**
>
> Hoopsnake is a component that enables feedback loops within Grasshopper. What it does in principle is to create a copy of the data it rec
