From: w wg Date: 2007-01-16T22:42:17+09:00 Subject: Re: unpack 4 bytes to a signed integer I tried your codes, it works exactly as what I expected. I havn't tried your library, but I think it should get the correct result too. Thank you. > The best way I've found is to unpack with N (to get the swapping right) > and then do some arithmetic to interpret the unsigned value as signed: > > x = -123 > s = [x].pack("N") > # Note that for _pack_ there is no need for a > # special signed version of N > > p s # ==> "\377\377\377\205" > > length = 32 > mid = 2**(length-1) > max_unsigned = 2**length > to_signed = proc {|n| (n>=mid) ? n - max_unsigned : n} > > p to_signed[s.unpack("N").first] # ==> -123