RosettaCodeData/Task/Bitwise-IO/Lingo/bitwise-io-2.lingo

38 lines
903 B
Plaintext

----------------------------------------
-- @param {string} str - ASCII string
-- @return {instance} BitArray
----------------------------------------
on crunchASCII (str)
ba = script("BitArray").new(str.length * 7)
pow2 = [64,32,16,8,4,2,1]
pos = 1
repeat with i = 1 to str.length
n = chartonum(str.char[i])
repeat with j = 1 to 7
ba.setBit(pos, bitAnd(n, pow2[j])<>0)
pos = pos+1
end repeat
end repeat
return ba
end
----------------------------------------
-- @param {instance} bitArray
-- @return {string} ASCII string
----------------------------------------
on decrunchASCII (bitArray)
str = ""
pow2 = [64,32,16,8,4,2,1]
pos = 1
cnt = bitArray.bitSize/7
repeat with i = 1 to cnt
n = 0
repeat with j = 1 to 7
n = n + bitArray.getBit(pos)*pow2[j]
pos = pos+1
end repeat
put numtochar(n) after str
end repeat
return str
end