Thought I'd share -- someone replied to a stack overflow post with the
following working solution:

  def to_byte_array(num)
    result = []
    begin
      result << (num & 0xff)
      num >>= 8
    end until (num == 0 || num == -1) && (result.last[7] == num[7])
    result.reverse
  end

Really appreciate everyone's willingness to help -- love the Ruby community!

On Fri, Mar 11, 2011 at 7:08 PM, James Miller <[email protected]> wrote:

> Thanks Stephen, I appreciate the help.
>
>
> On Fri, Mar 11, 2011 at 4:55 PM, Stephen Waits <[email protected]> wrote:
>
>> On 03/11/11 16:44, Stephen Waits wrote:
>>
>>>
>>> Here's another version that doesn't require you specify the number of
>>> bytes:
>>>
>>
>> Whoops, broken on negative numbers (and so obviously also on two's
>> complement of positive numbers) because my loop will never end.
>>
>> No time to hack on it now though. If nobody posts a proper solution, I'll
>> get you one this evening.
>>
>> Also..
>>
>> class Fixnum
>>  def twos_complement
>>    (~self) + 1
>>  end
>> end
>>
>>
>> --Steve
>>
>> --
>> SD Ruby mailing list
>> [email protected]
>> http://groups.google.com/group/sdruby
>>
>
>

-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby

Reply via email to