From Hex to string

p "66726f6d 70736e20".scan(/\w\w/).inject('') {|s, b| s << b.hex.chr}
=> "frompsn "

cheers

Simon

···

-----Original Message-----
From: Une bévue [mailto:pere.noel@laponie.com.invalid]
Sent: Wednesday, March 08, 2006 10:14 AM
To: ruby-talk ML
Subject: from Hex to string

i have datas like that :

646c6532 00000000 61657674 0000012e 00000000 00000000
0000011c 00000004
00000001 00000000 00000000 00000000 [...]
6c6f6e67 00000004 00000000 66726f6d 70736e20 00000008
00000000 005c0001
3b3b3b3b 2d2d2d2d 54455854 00000006 626d6b6d 3a76

seems to be hex encoded US-ASCII (?)

how to convert that in user readable us-ascii ? (to string)

i now i have the sequence "70736e20" which is "psn "
--
une bévue

fine, i do have about the same to translate into string.
this comes from event.data (event being a NSAppleEventDescriptor in
cocoa/MacOS X)

this is followed by :
"00000008 00000000"
which would have to be the psn number of the application having send the
event (i need this psn number in order to know which application is of
concern)

i know, because it's me having type an url into safari, by ps -ax giving
:

1347 ?? S 0:05.34 /Applications/Safari.app/Contents/MacOS/Safari
-psn_0_6029313

that, the psn number is "6029313" being an odd number, "00000008
00000000" an even number, afaik.

how could i convert that part "00000008 00000000" into an integer ?

i've make use of :
    keyword = 'addrpsn '.unpack('N').first
    sign=event.paramDescriptorForKeyword(keyword)
    p "sign.class = #{sign.class} - paramDescriptorForKeyword"
=> "sign.class = NilClass - paramDescriptorForKeyword"
    sign=event.descriptorForKeyword(keyword)
    p "sign.class = #{sign.class} - descriptorForKeyword"
=> "sign.class = NilClass - paramDescriptorForKeyword"

which would be the "proper" way to get the psn number but always fails
into nil class...

instead of doing that by scan, gsub etc...

however it appears to me NSAppleEventDescriptor don't give any way to
get pid, psn nor sign of the application having send this event : the
reason why, as a workaround, i 'm using this sort of hacking ))

···

Kroeger, Simon (ext) <simon.kroeger.ext@siemens.com> wrote:

p "66726f6d 70736e20".scan(/\w\w/).inject('') {|s, b| s << b.hex.chr}
=> "frompsn "

--
une bévue