Marcel Molina Jr. wrote:
> On Wed, Nov 23, 2005 at 07:37:25AM +0900, Daniel Sch?le wrote:
> 
>>Curt Hibbs wrote:
>>
>>>Does anyone have a clever way to convert an integer to an array of bit
>>>values?
>>>
>>>For example (using 8 bit integers):
>>>
>>>0 => [0, 0, 0, 0, 0, 0, 0, 0]
>>>1 => [0, 0, 0, 0, 0, 0, 0, 1]
>>>2 => [0, 0, 0, 0, 0, 0, 1, 0]
>>>7 => [0, 0, 0, 0, 0, 1, 1, 1]
>>>etc.
>>>
>>>I was looking at Array#pack and Array#unpack to do this, but haven't 
>>>figured
>>>it out yet. It just seems like there ought to be a simple way to do this.
>>>
>>>Anybody got a clever idea?
>>>
>>>Curt
>>
>>irb(main):082:0> x = 7.to_s(2).split(//).map! {|bit| bit.to_i}
>>=> [1, 1, 1]
>>irb(main):083:0> x.unshift(0) until x.length == 8
>>
>>if your number always fit into 8 bit
>>otherwise this doesn't work
> 
> 
> You could do this:
> 
> x.unshift(0) until (x.size % 8).zero?
> 
> marcel

Nah, that's too much work.

x = ("%08d" % 7.to_s(2)).split('').map{ |e| e.to_i }

Replace 8 with the integer size and 7 with whatever number you're converting.

Regards,

Dan