I am hoping I can get help with a project where I'm saving geolyzer data. The issue is how to make the data files small enough. My current method is to write the single digit density values 0, 1, 2, 3 into string form so that it's a string of 255 numbers. Then using the debug card's deflate function on it for final writing to the file. However, that's wasting a bunch of space since its saving the characters as 8bit values when I only need 2bit.
The new way I wish to use involves saving binary data. But not the standard 8bit files but a shorter 2bit data. As in saving the values 00, 01, 10, 11 to represent air, low density, intermediate density and high density respectively. But not having them write like 00000000, 00000001, 00000010, 00000011. Follows is my attempt at the code for saving 8bit values, unfortunately, I couldn't get it to work, though.
local fs = require "filesystem"
--create fake testing data
local testDensity = {}
local j = 0
for i=1,255 do
testDensity[i] = j
if j > 2 then
j = 0
else
j = j + 1
end
end
--opens test binary file
local file = io.open("testBinaryFile", "wb")
for i=1, 255 do
local bit
--attempt to convert the table's value into a binary value using the escape character \
if testDensity[i] == 0 then
bit = "\0"
elseif testDensity[i] == 1 then
bit = "\1"
elseif testDensity[i] == 2 then
bit = "\2"
else
bit = "\3"
end
file:write(bit)
end
io.close(file)
The result of this code is an empty file and I am unsure how to fix it to write the binary data, and then how to make that binary data 2bit and not 8bit.
Thank you for any assistance provided. I'm sorry if this post didn't follow the common guidelines, it is my first post on the site.
You can post now and register later.
If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.
Hello OC community,
I am hoping I can get help with a project where I'm saving geolyzer data. The issue is how to make the data files small enough. My current method is to write the single digit density values 0, 1, 2, 3 into string form so that it's a string of 255 numbers. Then using the debug card's deflate function on it for final writing to the file. However, that's wasting a bunch of space since its saving the characters as 8bit values when I only need 2bit.
The new way I wish to use involves saving binary data. But not the standard 8bit files but a shorter 2bit data. As in saving the values 00, 01, 10, 11 to represent air, low density, intermediate density and high density respectively. But not having them write like 00000000, 00000001, 00000010, 00000011. Follows is my attempt at the code for saving 8bit values, unfortunately, I couldn't get it to work, though.
The result of this code is an empty file and I am unsure how to fix it to write the binary data, and then how to make that binary data 2bit and not 8bit.
Thank you for any assistance provided. I'm sorry if this post didn't follow the common guidelines, it is my first post on the site.
Link to post
Share on other sites