From: Brian Candler Date: 2008-10-19T17:35:32+09:00 Subject: Re: Big endian convention in Ruby Zangief Ief wrote: > Because I would like to work with an input in binary format, I would > like to convert the message at the begining, before append the bit '1' > on it. In this goal, can I convert the data in message with this: > >>> message = "A message" > => "A message" >>> message.unpack('b*').join > => > "100000100000010010110110101001101100111011001110100001101110011010100110" > > There is .unpack('B*') too, but with "B" the order is not correct I > think. I believe you'll need B*. The letter "A" should unpack to 01000001 (MSB first). However this is a really, really bad way to implement the SHA1 algorithm. If the input is already presented as a string of bytes, then it is completely pointless to convert it into a string of bits, because the SHA1 algorithm is *designed* to be run on bytes, as the pseudocode demonstrates. That is one reason why the input has to be padded to a multiple of 64 bytes; so that the core loop does *not* have to worry about working at the bit level! Of course, as an academic exercise, you're free to do whatever you like. If you want to experiment with binary arithmetic where the operands are strings of 0x30 and 0x31 (representing bit 0 and bit 1 respectively), then fine. The resulting code will be tortuous, use tons of RAM and run extremely slowly. (Hopefully it should also be clear from the pseudocode that you don't have to read in the entire message at the start at all. You can process the message in 64-byte chunks, *as it arrives*) -- Posted via http://www.ruby-forum.com/.