From: Mike Stok Date: 2002-09-11T22:56:51+09:00 Subject: Re: Is there a "get" function for words and dwords? In article <9ba6a8da.0209110515.4b63b152@posting.google.com>, Khurram wrote: > The sysread returns a string... I want the actual binary value. > > Is there a way to do this conversion? [Sorry if this is a duplicate, I fat fingered something...] Unpack e.g. [mike@rand mike]$ od -c /bin/ls | head -1 0000000 177 E L F 001 001 001 \0 \0 \0 \0 \0 \0 \0 \0 \0 so the first two bytes are 0x7F and 0x45 (the ASCII value for E) irb(main):001:0> f = File.new('/bin/ls', 'r') # irb(main):002:0> bytes = f.sysread(2) "\177E" irb(main):003:0> bytes.unpack('n') [32581] irb(main):004:0> bytes.unpack('v') [17791] The difference between 'n' and 'v' in the template is the byte ordering used - 'n' is network (or big-endian) order, 'v' is little endian. The values here are 0x7F45 and 0x457F Unpack returns an array because it can be used to unpack many things at once. Hope this helps, Mike -- mike@stok.co.uk | The "`Stok' disclaimers" apply. http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60 http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA