Forum: Ruby How to reverse String.unpack('H*')

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2fce3b5a094cd28a0538bc4b314d1c09?d=identicon&s=25 Han Holl (Guest)
on 2006-05-21 11:49
(Received via mailing list)
Hello rubyists,

I have binary numbers that through an ODBC interface are decoded with
String.unpack('H*').

Let's give an example: the number 23.

b='xxxx'
b[0]=23; b[1] = b[2] = b[3] = 0

b.unpack('I')  =>  [23]
b.unpack('H*') => ["17000000"]

The problem: how do I convert "17000000" to 23 ?

The following works, but seems awfully complex:
fl = "17000000"
fl.unpack('a2a2a2a2').collect {|x|
"0x#{x}".hex}.pack('I').unpack('I')[0]

Any suggestions ?

Han Holl
4b174722d1b1a4bbd9672e1ab50c30a9?d=identicon&s=25 Ryan Leavengood (Guest)
on 2006-05-21 17:31
(Received via mailing list)
On 5/21/06, Han Holl <han.holl@gmail.com> wrote:
>
> The following works, but seems awfully complex:
> fl = "17000000"
> fl.unpack('a2a2a2a2').collect {|x| "0x#{x}".hex}.pack('I').unpack('I')[0]
>
> Any suggestions ?

irb(main):001:0> ["17000000"].pack('H*').unpack('I*')[0]
=> 23

Ryan
2fce3b5a094cd28a0538bc4b314d1c09?d=identicon&s=25 Han Holl (Guest)
on 2006-05-21 23:26
(Received via mailing list)
On 5/21/06, Ryan Leavengood <leavengood@gmail.com> wrote:
>
>
> irb(main):001:0> ["17000000"].pack('H*').unpack('I*')[0]
> => 23


Ah yes,  much better allready.

Thanks,

Han Holl
This topic is locked and can not be replied to.