Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local fs = require "filesystem"
- local string = require "string"
- local testDensity = {}
- local j = 0
- for i=0,255 do
- testDensity[i] = math.floor(math.random()*3)
- end
- local number1 = 0
- local number2 = 0
- local number3 = 0
- local number4 = 0
- --convert a passed number to base 4
- local function convToFour(num)
- number4 = math.floor(num)%4
- number3 = (math.floor(num/4))%4
- number2 = (math.floor(num/16))%4
- number1 = (math.floor(num/64))%4
- end
- --convert a passed number to base 10 from base 4, numbers must be in range of 0-3 for all places
- local function convToTen(num)
- local bTenNum1 = math.floor(num/1000)
- local bTenNum2 = math.floor((num-(bTenNum1*1000))/100)
- local bTenNum3 = math.floor((num-bTenNum1*1000-bTenNum2*100)/10)
- local bTenNum4 = math.floor(num-bTenNum1*1000-bTenNum2*100-bTenNum3*10)
- local bTenNum11 = bTenNum1*4^3
- local bTenNum22 = bTenNum2*4^2
- local bTenNum33 = bTenNum3*4^1
- local bTenNum44 = bTenNum4*4^0
- return(math.floor(bTenNum11+bTenNum22+bTenNum33+bTenNum44))
- end
- local str = ""
- for i=0,63 do
- local compressedNum = 0
- --turns 4 table values into a fake base 4 number that the convToTen can work with
- compressedNum = testDensity[i*4] * 1000
- compressedNum = compressedNum + testDensity[i*4+1] * 100
- compressedNum = compressedNum + testDensity[i*4+2] * 10
- compressedNum = compressedNum + testDensity[i*4+3]
- --then uses that number to write a character assosiated with it
- str = str..string.char(convToTen(compressedNum))
- end
- print(str)
- print(string.len(str))
- local file = io.open("testBinaryFile", "w")
- file:write(str)
- io.close(file)
- str = ""
- --tests to make sure it can write and read correctly
- local file = io.open("testBinaryFile", "r")
- str = file:read("*all")
- io.close(file)
- local recompiledString = ""
- for i = 1,64 do
- convToFour(string.byte(string.sub(str,i,i)))
- recompiledString = recompiledString..tostring(number1)
- recompiledString = recompiledString..tostring(number2)
- recompiledString = recompiledString..tostring(number3)
- recompiledString = recompiledString..tostring(number4)
- end
- print(testDensity[0]..table.concat(testDensity))
- print(recompiledString)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement