まつもと ゆきひろです In message "[ruby-dev:22349] Re: pack('N') broken on 64bit platforms" on 03/12/24, Yukihiro Matsumoto <matz / ruby-lang.org> writes: |In message "[ruby-dev:22344] pack('N') broken on 64bit platforms" | on 03/12/23, Minero Aoki <aamine / loveruby.net> writes: | ||64 ビットプラットフォーム上だと pack("N") の返り値が ||常に "\000\000\000\000" になります。 |Sparc64では |動くようです。エンディアンの違いかなあ。 そのようです。alpha-linuxなマシンでは再現しました。以下のパッ チが有効そうです。試してみてください。 --- pack.c 23 Dec 2003 10:02:15 -0000 1.61 +++ pack.c 23 Dec 2003 15:56:10 -0000 @@ -24,4 +24,2 @@ #ifdef NATINT_PACK -# define OFF16B(p) ((char*)(p) + (natint?0:(sizeof(short) - SIZE16))) -# define OFF32B(p) ((char*)(p) + (natint?0:(sizeof(long) - SIZE32))) # define NATINT_I32(x) (natint?NUM2LONG(x):(NUM2I32(x))) @@ -30,4 +28,4 @@ # ifdef WORDS_BIGENDIAN -# define OFF16(p) OFF16B(p) -# define OFF32(p) OFF32B(p) +# define OFF16(p) ((char*)(p) + (natint?0:(sizeof(short) - SIZE16))) +# define OFF32(p) ((char*)(p) + (natint?0:(sizeof(long) - SIZE32))) # endif @@ -52,7 +50,2 @@ -#ifndef OFF16B -# define OFF16B(p) (char*)(p) -# define OFF32B(p) (char*)(p) -#endif - #define define_swapx(x, xtype) \ @@ -777,3 +770,3 @@ pack_pack(ary, fmt) s = NATINT_HTONS(s); - rb_str_buf_cat(res, OFF16B(&s), NATINT_LEN(short,2)); + rb_str_buf_cat(res, OFF16(&s), NATINT_LEN(short,2)); } @@ -791,3 +784,3 @@ pack_pack(ary, fmt) l = NATINT_HTONL(l); - rb_str_buf_cat(res, OFF32B(&l), NATINT_LEN(long,4)); + rb_str_buf_cat(res, OFF32(&l), NATINT_LEN(long,4)); } @@ -1656,3 +1649,3 @@ pack_unpack(str, fmt) unsigned short tmp = 0; - memcpy(OFF16B(&tmp), s, NATINT_LEN(unsigned short,2)); + memcpy(OFF16(&tmp), s, NATINT_LEN(unsigned short,2)); s += NATINT_LEN(unsigned short,2); @@ -1667,3 +1660,3 @@ pack_unpack(str, fmt) unsigned long tmp = 0; - memcpy(OFF32B(&tmp), s, NATINT_LEN(unsigned long,4)); + memcpy(OFF32(&tmp), s, NATINT_LEN(unsigned long,4)); s += NATINT_LEN(unsigned long,4);