Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #2427 > unrolled thread
| Started by | "Aaron D. Gifford" <astounding@gmail.com> |
|---|---|
| First post | 2011-04-06 23:56 -0500 |
| Last post | 2011-04-07 11:30 -0500 |
| Articles | 4 — 2 participants |
Back to article view | Back to comp.lang.ruby
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
| From | "Aaron D. Gifford" <astounding@gmail.com> |
|---|---|
| Date | 2011-04-06 23:56 -0500 |
| Subject | Packing and unpacking unsigned integers of arbitrary size as binary strings |
| Message-ID | <BANLkTimL4u2=2quBF-qv0x2N7W7NJ8u9AQ@mail.gmail.com> |
I see there's a 'w' option to pack for packing arbitrary sized unsigned integers in BER compressed format. However I need to pack/unpack binary strings as simple binary byte strings. The results of my own needs have been gemmified as the bignumpack gem. See https://github.com/astounding/bignumpack/blob/master/lib/bignumpack.rb Are there alternatives that may be more efficient, or if I've missed something obvious. I resorted to splitting the binary string into 64-bit sized chunks and using the 'Q' packing option (except where I couldn't determine endianness, where I resorted to using the 32-bit network-order 'N' packing option). While I tried to be endian-architecture friendly, I don't have a big-endian box with Ruby on it handy to test and would appreciate it if anyone on a big-endian box would let me know if the tests fail. Aaron out.
[toc] | [next] | [standalone]
| From | Brian Candler <b.candler@pobox.com> |
|---|---|
| Date | 2011-04-07 04:41 -0500 |
| Message-ID | <bd019337c95c63d99848674eb23125b0@ruby-forum.com> |
| In reply to | #2427 |
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/.
[toc] | [prev] | [next] | [standalone]
| From | Brian Candler <b.candler@pobox.com> |
|---|---|
| Date | 2011-04-07 04:43 -0500 |
| Message-ID | <b9069dd06df1d33c8252f733f1008839@ruby-forum.com> |
| In reply to | #2440 |
Oh, I forgot to do the reverse:
>> bin = "\253T\251\214\353\037\n\322"
=> "\253T\251\214\353\037\n\322"
>> bin.unpack("H*").first.to_i(16)
=> 12345678901234567890
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | "Aaron D. Gifford" <astounding@gmail.com> |
|---|---|
| Date | 2011-04-07 11:30 -0500 |
| Message-ID | <BANLkTimQyh6eMVuRh+GVyjxgz+zzyRduiQ@mail.gmail.com> |
| In reply to | #2441 |
Nice, using the conversion via hex string is faster for number-to-binary-string conversion, but slower in string-to-number conversion on my system. I think I'll update and use it for number-to-string. Thanks! Aaron out.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web