1

I wish to encode arbitrary binary data in an array of bytes. However the valid values for the data in each encoded byte is in the range of 1-127 inclusive. In other words, invalid values of encoded bytes are 0 and 128-255. With other words, I can't use the most significant bit (it has to be zero) and the value cannot be 0. So there 126 usable values to encode with in each byte. I know how to solve this using 6 bits per byte, simply storing data in bits 2-7, leaving bit 1 always 1 and bit 8 always 0. But I'd like to do better.

I have a feeling this is a "base of" question, as my previous solution with 6 bits seem similar to base64, but with a different table. So is this what I would like to look into called base126 encoding?

Why I want to use this base126 encoding (?) scheme is because I'd like to improve my Lua scripts in a game called Dual Universe, where data transmission is limited between emitters and receivers. For some reason the game can't handle a string with a null character in it, and strings (which are used for transmission) are utf8 encoded so I don't want to touch the most significant bit (or it would expect multibyte character data). I want to send binary data over this string with these limitations.

I don't know if I am barking up the tree but I have a feeling I should kind of think of the data I want to encode as a large number and divide and modulus by 126 and add 1, but I don't know how to work large arrays like that.

I'd be happy with a pointer into looking somewhere dealing with storing data in this way, if possible. I grok some oop/procedual languages so it doesn't have to be lua specific. Meanwhile I'll look into base85, because that sounds similar in ways to what I want to do based on my own guesswork. Maybe I get some insights there.

Statement
  • 3,888
  • 3
  • 36
  • 45
  • 3
    You could still simply use base64, which is made for this exact purpose (binary payloads in a limited environment - ASCII suffices). Yes, it could theoretically be made (slightly) more efficient, but it probably isn't worth the effort. base126 can't be implemented as efficiently / elegantly because 126 is not a power of two. – Luatic Apr 08 '23 at 13:41
  • 1
    If you really want to do this, grab a bignum library for lua, convert input from base X to a giant number, output in base Y. – teapot418 Apr 08 '23 at 13:48
  • Thank you for your inputs. I have been asking ChatGPT also, and the answers there were also using multiprecision math libraries. I think I'll have to settle for base64. – Statement Apr 08 '23 at 14:16

0 Answers0