I need to unpack the string of packed ASCII characters. The algorithm is following:
- Get 3 packed ASCII bytes, put them into 4 ASCII bytes. (6 + 6 + 6 + 6 bits in 3 bytes to 4 bytes)
- For each byte, set bit 6 as complement to bit 5.
- For each byte reset bit 7 to zero.
- Repeat for next 3 packed bytes.
I'm new to Ruby, may be there is more correct and elegant way to solve this task, rather my code:
while i < pstr.length
parr = [pstr[0] & 0x3F, pstr[0]>>6 | ((pstr[1] << 2 ) & 0x3F),
pstr[1]>>4 | ((pstr[2] << 4 ) & 0x3F),
pstr[2]>>2]
parr.collect! { |a| a | (~(a << 1) & 0x20) }
parr.collect! { |a| a & 0x7F }
puts parr
i += 3
end
Update1: Thx for correction of collect.
#unpack? ruby-doc.org/core-2.1.1/String.html#method-i-unpack