*EDIT* I've now got it officially working! I *think* the problem was the location of the concatenation, but I changed a lot and am not sure. You can paste this full item code into an Advanced Item > Script widget in GHI and it will work (type /binary to speak)
It allows you to both speak and understand binary.
I don't think the runtime of lua was the problem, after all that. Thanks for looking it over!
Code:
function toBits(num,bits)
-- function toBits(bits)
-- returns a table of bits, most significant first.
bits = bits or select(2,math.frexp(num))
local t={} -- will contain the bits
for b=bits,1,-1 do
t[b]=math.fmod(num,2)
num=(num-t[b])/2
end
return t
end
local binString = ""
function cmdSay(msg)
-- local msg = "how crazy can we get?!`1234567890~!@#$%^&*("
local array1 = {}
binString = ""
for ltr in msg:gmatch(".") do table.insert(array1, ltr) end
for k,v in pairs(array1) do
local binNum
if v == " " then
binNum = " "
else
binNum = table.concat(toBits(string.byte(v)))
end
binString = binString .. binNum
end
GHI_Say("[Binary]: "..binString)
end
local binary = GHI_SlashCmd("binary")
binary.SetDefaultFunc(function(x)
cmdSay(x)
end
)
function humanize(message)
local word_table = {}
local array2 = {}
local array3 = {}
local strip = 0
for wurd in message:gmatch("%S+") do if strip>0 then table.insert(array3, wurd) else end strip = strip + 1 end
for k,v in pairs(array3) do
-- print('beg of pairs', k, v)
local incr = 1
local array4 = {}
local tByte = ""
local altr = ""
local actual_word = ""
for w in string.gmatch(v, ".") do
tByte = tByte .. w
local sum = 0
if incr%7==0 then
array4[incr/7] = tByte
local bin = string.reverse(array4[incr/7])
for i = 1, string.len(bin) do
num = string.sub(bin, i,i) == "1" and 1 or 0
sum = sum + num * math.pow(2, i-1)
end
tByte=""
altr = string.char(sum)
actual_word = actual_word .. altr
end
incr = incr + 1
end
word_table[k] = actual_word
-- print('end of pairs', k, v)
end
local sentence = ""
for ke,va in pairs(word_table) do
-- print(ke, va)
sentence = sentence .. " " .. va
end
print(sentence)
end
if not binbrewFrame then
binbrewFrame = CreateFrame("Frame");
end
binbrewFrame:RegisterEvent("CHAT_MSG_SAY");
local binBrewFunc = function (self, event, ...)
local message, sender, language, channelString, target, flags, unknown, channelNumber, channelName, unknown, counter = ...
local msgSearch = string.find(message, "[bB]inary");
-- if GHI_CountBuffs("Binary") > 0 then
if msgSearch ~= nil then
humanize(message)
end
-- end
end
binbrewFrame:SetScript("OnEvent", binBrewFunc);