w wg
1/16/2007 1:42:00 PM
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