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?
How to turn binary into characters and viceversa
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How to turn binary into characters and viceversa
Hi! I wish you have an amazing day!
- 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
For one, you mean {1,1,0,1,0,1}, note the brackets; second, assuming that is a binary string, you can do this:
Be aware that this isn't completely UTF-8 compatible, since it just returns 8-bit characters.
Code: Select all
string.byte(tonumber(table.concat{1,1,0,1,0,1}, 2))
Me and my stuff True 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.
Re: How to turn binary into characters and viceversa
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.
You may have to reverse the table if the number is big or little endian.
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)
Re: How to turn binary into characters and viceversa
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!
Re: How to turn binary into characters and viceversa
For 8-bit character bytes:
Or for any number of bits:
Code: Select all
local bits = {}
for i = 1, 8 do
bits[i] = bit.band(byte, 2^(8-i)) > 0 and 1 or 0
end
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
- 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
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
Me and my stuff True 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.
Re: How to turn binary into characters and viceversa
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!
- 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
luajit's own website, but it's not needed for this thing, you can just and it'll definitely work.
Code: Select all
bit = require 'bit'
Me and my stuff True 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.
Re: How to turn binary into characters and viceversa
Thanks Zorg!zorg wrote: ↑Sun Jan 26, 2020 8:23 am luajit's own website, but it's not needed for this thing, you can justand it'll definitely work.Code: Select all
bit = require 'bit'
Hi! I wish you have an amazing day!
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 5 guests