Byte Array

0 and 1 are currently integers, that means the number 0 is 00000000000000000000000000000000 and 1 is 00000000000000000000000000000001, 2 is 00000000000000000000000000000010, etc. (the first bit is positive/negative numbers. Can we, instead of 00000…0 and 0000…1 simply use 0 and 1.

Example:

function setup()
      i = 5 --Integer
      b = byte(0) --1-Byte-Level
end

PS. I searched.

@TokOut I don’t understand what you want. What are you doing that you’re getting all those leading zeroes. It looks like you’re after binary values. If you want to convert a decimal number to binary then try this. There are no leading zeroes.

function setup()
    a=toBin(10000)
    print(a)
end

function toBin(n)
    local digits=""
    while n>0 do
        digits=digits..n%2
        n=n//2
    end
    return string.reverse(digits)
end

If you have a number of the type integer, you have 32 bytes storing its’ data.

Eg:

integer 2 = byte 00000000000000000000000000000010
integer 5 = byte 00000000000000000000000000000101
integer 17 = byte 00000000000000000000000000010001
integer -4 = byte 10000000000000000000000000000100

But I need a byte array of, for example not 32 bytes (integer), I need n-bytes.

Try this.

function setup()
    a=toBin(100,6)
    print(a)
end

function toBin(n,s) -- n=decimal number   s=number of digits to show
    if n<0 then
        n=-n
        neg=true
    end
    local digits=""
    while n>0 do
        digits=(n%2)..digits
        n=n//2
    end
    if #digits>s or #digits>=s and neg then
        return "error, not enough digits"
    end
    digits=string.rep("0",s)..digits
    if neg then
        digits="1"..string.sub(digits,#digits-s+2,#digits)
    else
        digits=string.sub(digits,#digits-s+1,#digits)
    end
    return digits
end

Thank you!