Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2440

Re: Packing and unpacking unsigned integers of arbitrary size as binary strings

From Brian Candler <b.candler@pobox.com>
Newsgroups comp.lang.ruby
Subject Re: Packing and unpacking unsigned integers of arbitrary size as binary strings
Date 2011-04-07 04:41 -0500
Organization Service de news de lacave.net
Message-ID <bd019337c95c63d99848674eb23125b0@ruby-forum.com> (permalink)
References <BANLkTimL4u2=2quBF-qv0x2N7W7NJ8u9AQ@mail.gmail.com>

Show all headers | View raw


Aaron D. Gifford wrote in post #991376:
> Are there alternatives that may be more efficient, or if I've missed
> something obvious.

I'd just do this:

>> num = 12345678901234567890
=> 12345678901234567890
>> hex = num.to_s(16)
=> "ab54a98ceb1f0ad2"
>> hex.size
=> 16
>> [hex].pack("H*")
=> "\253T\251\214\353\037\n\322"

Add an extra '0' to the left of hex if it's an odd number of characters.

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Packing and unpacking unsigned integers of arbitrary size as binary strings "Aaron D. Gifford" <astounding@gmail.com> - 2011-04-06 23:56 -0500
  Re: Packing and unpacking unsigned integers of arbitrary size as binary strings Brian Candler <b.candler@pobox.com> - 2011-04-07 04:41 -0500
    Re: Packing and unpacking unsigned integers of arbitrary size as binary strings Brian Candler <b.candler@pobox.com> - 2011-04-07 04:43 -0500
      Re: Packing and unpacking unsigned integers of arbitrary size as binary strings "Aaron D. Gifford" <astounding@gmail.com> - 2011-04-07 11:30 -0500

csiph-web