From: Tony Headford Date: 2013-02-04T01:04:51+09:00 Subject: Re: Can anyone tell me the computational logic of Unpack() method of string? What is it you don't understand exactly ? Unpack decodes a string based on the format you specify and returns an array of the decoded elements. The format specifier is made up of a single character and may be followed by a number indicating how many times to repeat the format specifier, or a '*' to indicate all remaining elements. In the examples you provided: 1. 'A'.unpack('b*') Will return a string representation of the binary value of 'A' in LSB first order. 2. 'A'.unpack('B*') Will return a string representation of the binary value of 'A' in MSB first order. 3. "hello".unpack('B*') This returns a string representation of the binary values 'h', 'e', 'l', 'l', 'o' in MSB first order 4. "hello".unpack('C*').map { |e| e.to_s 2 } Unpack creates an array of the integer values of 'h', 'e', 'l', 'l', 'o'. This is then iterated over to create a new array by converting each value to a string using Base2 or binary format. Does that help ? cheers, Tony. On 3 Feb 2013, at 11:58, Arup Rakshit wrote: >> $ 'A'.unpack('b*') >> => ["10000010"] >> >> $ 'A'.unpack('B*') >> => ["01000001"] >> >> $ "hello".unpack('B*') >> => ["0110100001100101011011000110110001101111"] >> >> $ "hello".unpack('C*').map {|e| e.to_s 2} >> => ["1101000", "1100101", "1101100", "1101100", "1101111"] >> > >> But couldn't understand the logic. So any help would be appreciated! >> Thanks, > > Can anyone help me here to understand? > > -- > Posted via http://www.ruby-forum.com/. >