This doesn't get you a byte array, but here's how I'd get a binary
representation of a number using two's compliment notation.

sprintf('%08b', number)
where 8 is the number of bit's in your array.

you have to specify the number of bits in order to get a two's
compliment that works, right?

10.downto(-10) do |number|
  p number # the number
  p number.to_s(2) #as binary (with sign)
  p sprintf('%b', number)  #as binary, with 'two's complement' prefixed by ..1
  p sprintf('%08b', number)  # as a nice proper two's complement number,
  p "---"
end

-Graeme

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

Reply via email to