Page 1 of 1

How to turn binary into characters and viceversa

Posted: Sat Jan 25, 2020 5:25 am
by Darlex
How can I turn an array like [1,1,0,1,0,1] into a character? And how do I turn it back into an bit array like that one I already showed?
Is there an "easy" way to do it or do I need to do some LuaJIT trickery?

Re: How to turn binary into characters and viceversa

Posted: Sat Jan 25, 2020 5:49 am
by zorg
For one, you mean {1,1,0,1,0,1}, note the brackets; second, assuming that is a binary string, you can do this:

Code: Select all

string.byte(tonumber(table.concat{1,1,0,1,0,1}, 2))
Be aware that this isn't completely UTF-8 compatible, since it just returns 8-bit characters.

Re: How to turn binary into characters and viceversa

Posted: Sat Jan 25, 2020 10:24 am
by ivan
Beautiful solution by zorg.
My only concern would be the maximum number size that "tonumber" can handle. It will probably work fine for the 0-255 range.
A "safer" solution would be to iterate the table.

Code: Select all

local sum = 0
  for i = 1, #bin do
  sum = sum + bin[i]*math.pow(2, i - 1)
end
local byte = string.byte(sum)
You may have to reverse the table if the number is big or little endian.

Re: How to turn binary into characters and viceversa

Posted: Sat Jan 25, 2020 1:13 pm
by Darlex
Woah thanks zorg and ivan! These are wonderful solutions! But how can I do the opposite? Like turning a character into a binary array?

Re: How to turn binary into characters and viceversa

Posted: Sat Jan 25, 2020 2:37 pm
by grump
For 8-bit character bytes:

Code: Select all

local bits = {}
for i = 1, 8 do
	bits[i] = bit.band(byte, 2^(8-i)) > 0 and 1 or 0
end
Or for any number of bits:

Code: Select all

local bits = {}
local nbits = math.ceil(math.log(val) / math.log(2))
for i = 1, nbits do
	bits[i] = bit.band(val, 2^(nbits-i)) > 0 and 1 or 0
end

Re: How to turn binary into characters and viceversa

Posted: Sat Jan 25, 2020 9:02 pm
by zorg
you might need to include the bit library to use those functions though... maybe; i usually do, but it might not be necessary; if it errors, you'll know anyway :3

Re: How to turn binary into characters and viceversa

Posted: Sun Jan 26, 2020 2:58 am
by Darlex
zorg wrote: Sat Jan 25, 2020 9:02 pm you might need to include the bit library to use those functions though... maybe; i usually do, but it might not be necessary; if it errors, you'll know anyway :3
Sorry for being an absolute noob in this lua* (jit/ffi/etc) stuff, where can i get some info?

Re: How to turn binary into characters and viceversa

Posted: Sun Jan 26, 2020 8:23 am
by zorg
luajit's own website, but it's not needed for this thing, you can just

Code: Select all

bit = require 'bit'
and it'll definitely work.

Re: How to turn binary into characters and viceversa

Posted: Mon Jan 27, 2020 4:51 am
by Darlex
zorg wrote: Sun Jan 26, 2020 8:23 am luajit's own website, but it's not needed for this thing, you can just

Code: Select all

bit = require 'bit'
and it'll definitely work.
Thanks Zorg!