keichi / binary-parser

A blazing-fast declarative parser builder for binary data
MIT License
857 stars 133 forks source link

Variable length decoding #257

Open NickStallman opened 1 month ago

NickStallman commented 1 month ago

How would you go about decoding a variable length format, like just for example UTF-8?

It needs to:

  1. Read a single byte, if the values are between x and y then it's a single byte
  2. Else read a second byte and then treat it as a single 16 bit value
  3. This process could continue

My specific use case has a single byte command, except when that byte is 0xfe in which case it's a two byte command. It's a very rudimentary case where more than 256 commands were needed so they just created a 2nd list of 256 commands.