Binary DataStores?

Is there a way to store data as binary to save space?

I was originally planning on using string.char to convert a byte into a string, but then I realized that characters only range from 0 to 255. Obviously this won’t do when the full range of binary is far beyond that. I’ve found things like string.pack, but I just can’t seem to find any straight forward answers on how I could use this for DataStores as they save as a string.

One moment!

Aaaand… There it is:


That does seem useful, but I’m just wanting to save raw bytes. There’s a lot of data a single byte can store with all of the different possible combinations.

Reasonable enough! Then you can use the bit32 library.

Or, alternatively, you can write your own converter from a number into a binary string and then back. That would probably even be easier than making sense of the aforementioned bit32 library.

1 Like

I was going to say, the bit32 library doesn’t make much sense to me. It did show an example of 0xffffffff though. I’m new to binary and all of this stuff so I’m left a bit confused. If that’s a way that binary is written, will DataStores still consider it the same or would it still convert into a string? When I encode into json or print it, it just returns a number.

This bit at the bottom worries me about all data being saved as a string.

You can save it as a number in a DataStore, but it’s unknown what’s done under the hood after that. And that’s probably why writing your own binary converter is better.

Actually, it’s pretty easy:

Say, we have the number 0. It can be represented as one bit, because each bit is either 0 or 1.
But if we have a number 2, thats where it gets more interesting. You’re going to have to add another bit, and it’ll look something like this:

0 - 0
1 - 1
00 - 2
01 - 3
10 - 4
11 - 5

You can just keep adding more and more bits the larger your value is. To get how many numbers x bits can store, you can do 2^x. And if you want to know how many bits a value needs, you can do

local function BitsNeeded(value)
    return math.ceil(math.log(value) / math.log(2))

print(BitsNeeded(256)) -- 8

This should be enough to get you started. Hopefully this will be useful!

1 Like

Oh, you just made me realize that I was thinking of this all wrong. 0-255 is indeed enough because of a byte being able to store up to 256. I was thinking of the number of combinations instead of what number it would convert to. Thank you.

1 Like