How to turn binary into characters and viceversa

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Darlex
Party member
Posts: 128
Joined: Sun Sep 24, 2017 10:02 am
Location: Chile
Contact:

How to turn binary into characters and viceversa

Post 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?
Hi! I wish you have an amazing day!
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to turn binary into characters and viceversa

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: How to turn binary into characters and viceversa

Post 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.
Darlex
Party member
Posts: 128
Joined: Sun Sep 24, 2017 10:02 am
Location: Chile
Contact:

Re: How to turn binary into characters and viceversa

Post 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?
Hi! I wish you have an amazing day!
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: How to turn binary into characters and viceversa

Post 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
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to turn binary into characters and viceversa

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Darlex
Party member
Posts: 128
Joined: Sun Sep 24, 2017 10:02 am
Location: Chile
Contact:

Re: How to turn binary into characters and viceversa

Post 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?
Hi! I wish you have an amazing day!
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to turn binary into characters and viceversa

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Darlex
Party member
Posts: 128
Joined: Sun Sep 24, 2017 10:02 am
Location: Chile
Contact:

Re: How to turn binary into characters and viceversa

Post 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!
Hi! I wish you have an amazing day!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 15 guests