[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Unpack problems

Aaron Patterson

12/9/2005 9:18:00 PM

Hi! I'm having some troubles with unpack that I can't seem to figure
out. My test program looks like this:

foo = "\000\000\022\227"
puts foo.unpack("N")

On linux (ruby 1.8.3 (2005-09-21) [i386-linux]), the test program gives
the following output:

4759

Which is what I expect. However, on OS X (ruby 1.8.2 (2004-12-25)
[powerpc-darwin8.0]), I get the following output:

2534539264

I can't figure out if I'm doing something stupid, or if there is a
problem with ruby. Thanks for the help!

--Aaron



3 Answers

Wilson Bilkovich

12/9/2005 9:31:00 PM

0

This looks like an endianness issue. Check out what happens when you
reverse it:
puts foo.reverse.unpack("N")

"N" indicates Network byte order (big-endian), but it sounds like you
may want to use system byte-order, which is "L"
I don't have a Mac to test with, unfortunately. Check out page 603 in
the Pickaxe book, if you have it.

--Wilson.

On 12/9/05, Aaron Patterson <aaron_patterson@speakeasy.net> wrote:
> Hi! I'm having some troubles with unpack that I can't seem to figure
> out. My test program looks like this:
>
> foo = "\000\000\022\227"
> puts foo.unpack("N")
>
> On linux (ruby 1.8.3 (2005-09-21) [i386-linux]), the test program gives
> the following output:
>
> 4759
>
> Which is what I expect. However, on OS X (ruby 1.8.2 (2004-12-25)
> [powerpc-darwin8.0]), I get the following output:
>
> 2534539264
>
> I can't figure out if I'm doing something stupid, or if there is a
> problem with ruby. Thanks for the help!
>
> --Aaron
>
>
>


Aaron Patterson

12/9/2005 9:38:00 PM

0

On Sat, Dec 10, 2005 at 06:30:33AM +0900, Wilson Bilkovich wrote:
> This looks like an endianness issue. Check out what happens when you
> reverse it:
> puts foo.reverse.unpack("N")
>
> "N" indicates Network byte order (big-endian), but it sounds like you
> may want to use system byte-order, which is "L"
> I don't have a Mac to test with, unfortunately. Check out page 603 in
> the Pickaxe book, if you have it.
>
> --Wilson.
>

Looks like it is an endianness issue. Thank you for the help!

--Aaron



Eric Hodel

12/9/2005 10:06:00 PM

0

On Dec 9, 2005, at 1:17 PM, Aaron Patterson wrote:

> Hi! I'm having some troubles with unpack that I can't seem to figure
> out. My test program looks like this:
>
> foo = "\000\000\022\227"
> puts foo.unpack("N")
>
> On linux (ruby 1.8.3 (2005-09-21) [i386-linux]), the test program
> gives
> the following output:
>
> 4759
>
> Which is what I expect. However, on OS X (ruby 1.8.2 (2004-12-25)
> [powerpc-darwin8.0]), I get the following output:
>
> 2534539264
>
> I can't figure out if I'm doing something stupid, or if there is a
> problem with ruby. Thanks for the help!

Tiger's Ruby's pack/unpack have reversed endianness. You'll have to
build your own Ruby for it to work correctly.

--
Eric Hodel - drbrain@segment7.net - http://se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...