From: Robert Klemme Date: 2004-09-23T17:44:50+09:00 Subject: Re: negative numbers and binary formats "Florian Gross" schrieb im Newsbeitrag news:2rduavF18hbhuU1@uni-berlin.de... > Paul wrote: > > Moin! > > > Im trying to take a negative integer value, convert it to its binary > > equivalent, and save its hex value > > > > -7 -> 1111 1001 -> F9 > > Does this help? > > irb(main):032:0> [-7].pack("c").unpack("H*").first > => "f9" That's nice although it is somewhat limited regarding the size of values: >> [-7000].pack("c").unpack("H*").first => "a8" After a bit experimenting I came up with this: >> [-7].pack("i").unpack("h*").shift.reverse.gsub(/^f+(?=f)/, '') => "f9" >> [-7000000].pack("i").unpack("h*").shift.reverse.gsub(/^f+(?=f)/, '') => "f953040" Florian, what do you think? Kind regards robert