Storing large numbers in a string

1.I want to store big amount of bits in a string. The method I am using is changing each byte to a character and then plug it on the string.

  1. The problem is I am not sure how to make a get bits function that gives me the bits from i to j, why I am not sure how to do it?, well because lets say i = 6 j = 12, then I will need to get the first byte and the second byte and not all of the bits inside, which is a thing i don’t know how to do.

``

local str = ""
function ByteChunks(int) -- returns 4 bytes from that integer
    local byte1 = bit32.band(int,255)
    bit32.rshift(int,8)
    local byte2 = bit32.band(int,255)
    bit32.rshift(int,8)
    local byte3 = bit32.band(int,255)
    bit32.rshift(int,8)
    local byte4 = bit32.band(int,255)
    bit32.rshift(int,8)
    return byte1,byte2,byte3,byte4
end
function Convert(int) -- converts the bytes to a character and stores them into the string
    local byte1,byte2,byte3,byte4 = ByteChunks(int)
    local c1 = string.char(((byte1)%256))
    local c2 = string.char(((byte2)%256))
    local c3 = string.char(((byte3)%256))
    local c4 = string.char(((byte4)%256))
    str..=c4
    str..=c3
    str..=c2
    str..=c1
end

function getbits(i,j)
    --// this is the function idk how to make    
end
local int = 24242
print(bit32.extract(int,6,12))--// such that it does the same as what this does

I do know how to get the bits from the byte but I don’t know what bytes i should get such that the bits from i to j are in.

1 Like