Pack and unpack an array : unpack « Array « Ruby






Pack and unpack an array


a = [1,2,3,4,5,6,7,8,9,10]  
b = a.pack('i10')           # Pack 10 4-byte integers (i) into binary string b
c = b.unpack('i*')          # Decode all (*) the 4-byte integers from b
c == a                      # => true

 

Related examples in the same category