From: nobu.nokada@... Date: 2003-05-29T08:37:11+09:00 Subject: Re: RCR: unpack/pack Bignum Hi, At Thu, 29 May 2003 07:20:04 +0900, Robert Feldt wrote: > No one seems to be interested in this issue so I'll have to reply to > myself... ;) I've thought about same thing (and Bignum constructor from binary data), but not found good template characters for them. > It should work for both big and little endian architectures but I've only > tried on little endian. Would be great if someone can try on big-endian > machine. > > This patch adds a 'W' template character to pack and unpack for > packing/unpacking an unsigned integer (Fixnum *OR* Bignum). The packing > is from MSB to LSB so that > > [0xff00].pack("W") == "\377\000" I guess Bignum (un)packing would be used for external data exchange, so it'd be better to provide each singned/unsigned and big/little endian conversion, like as 'l', 'L' and so on. But 'w' is used already. > I choose W as in "raW binary representation of number" but its hard to > find a good template char since most are taken. Agree. A nitpicking: > - len = RBIGNUM(from)->len * SIZEOF_BDIGITS; > - { > - char tmp[len]; > - > - rb_nonneg_bignum_pack(tmp, from); > - // Skip leading zeroes if positive bignum. Extend > - // this "strategy" for 'w' so that only negative > - // bignums (and 0) can have leading zero? Variable length array and C++ style comment are C99 feature (or a compiler's extension). -- Nobu Nakada