How do I decode a string of 8 bit bytes

What happens if #bytes % 1024 is not equal to zero? What happens to the remaining bytes?

They just get added as a string at the end, even if it isn’t the full length.

1 Like

Oh also here it says the pattern is too complex

local pattern = "."..string.rep(".?", csize - 1)

for m in str:gmatch(pattern) do
		table.insert(out, m)
	end

Sorry about that, I have written a different formula that might work better:

local insert = table.insert

local function split(str, csize)
	local out = {}
	
	for i=1,str:len(),csize do
	insert(out, str:sub(i, i + csize - 1))
	end
	
	return out
end

print(table.concat(split("test", 3), "\n"))
1 Like

Oh no need to apologize mate, sorry just want more thing.

So now that the bytes are split up, I tried to convert each byte into its value and then normalize it but it will return me only zero

local function split(str, csize)
	local out = {}
	
	for i=1,str:len(),csize do
        local value = str:sub(i, i + csize - 1):byte(1,-1)
        local normalized = (value - 128) / 127.0

	    table.insert(out, normalized)
	end
	
	return out
end

local chunks = split(byteString, 1024)

print(chunks[10])

which is weird because if I did this, it will spit out all the numbers correctly (128,128,128,…)

local function split(str, csize)
	local out = {}
	
	for i=1,str:len(),csize do
	    table.insert(out, str:sub(i, i + csize - 1))
	end
	
	return out
end

local chunks = split(byteString, 1024)

print(chunks[10]:byte(1,-1))

Hopefully this function should cover all your needs:

local insert = table.insert

local function split_and_normalise(str)
	local out = {}
	local csize = 1024
	
	for i=1,str:len(),csize do
		local res = {str:sub(i, i + csize - 1):byte(1, -1)}
		for i=1,#res do
			insert(out, (res[i] - 128) / 127.0)
		end
	end
	
	return out
end

print(table.concat(split_and_normalise("test"), "\n"))
1 Like

Yeah, it still returns zero. Just wondering are you returning an entire string for each chunk?

local function split_and_normalise(str)
	local out = {}
	local csize = 1024
	
	for i=1,str:len(),csize do
		local res = {str:sub(i, i + csize - 1):byte(1, -1)}
		for i=1,#res do
			table.insert(out, (res[i] - 128) / 127.0)
		end
	end
	
	return out
end

local chunks = split_and_normalise(byteString, 1024)

print(chunks[10])

what am i looking at … roblox went from obbys to this?

1 Like

To use it, just enter the string, no need for chunk size this time, and it returns a table of all the normalised values, so no need to index the table.

It takes in a hex string (with the hex indicators \x), converts them to bytes, then converts the bytes to a float array.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.