Is there a "get" function for words and dwords?

Hi all,

.getc can be used to read characters (1 byte) at a time. Is there
anything that will read a word or dword at a time?

Currently I’m getting around it by doing:

data16 = a.getc | (a.getc << 8)

Thanks,

  • Khurram

What about sysread with the appropriate byte count as an argument?

Hope this helps,

Mike

···

In article 9ba6a8da.0209101035.13897026@posting.google.com, Khurram wrote:

.getc can be used to read characters (1 byte) at a time. Is there
anything that will read a word or dword at a time?


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

.getc can be used to read characters (1 byte) at a time. Is there
anything that will read a word or dword at a time?

Currently I’m getting around it by doing:

data16 = a.getc | (a.getc << 8)

String/Array pack() and unpack()

±-- Kero ------------------------------ kero@chello.nl —+

Don’t split your mentality without thinking twice |
Proud like a God – Guano Apes |
±-- M38c ---------- http://httpd.chello.nl/k.vangelder —+

Mike Stok mike@stok.co.uk wrote in message news:slrnansfor.cjb.mike@ratdog.stok.co.uk

···

In article 9ba6a8da.0209101035.13897026@posting.google.com, Khurram wrote:

.getc can be used to read characters (1 byte) at a time. Is there
anything that will read a word or dword at a time?

What about sysread with the appropriate byte count as an argument?

Hope this helps,

Mike

The sysread returns a string… I want the actual binary value.

Is there a way to do this conversion?

  • Khurram

[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’)
#<File:/bin/ls>
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

···

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?


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

Hi,

.getc can be used to read characters (1 byte) at a time. Is there
anything that will read a word or dword at a time?

What about sysread with the appropriate byte count as an argument?

Hope this helps,

Mike

The sysread returns a string… I want the actual binary value.

Is there a way to do this conversion?

If you mean 2 byte data by “word” and 4 byte data by “dword”,

class IO
def getword
read(2).unpack(“s”).first
end
def getdword
read(4).unpack(“l”).first
end
end

···

At Wed, 11 Sep 2002 22:36:44 +0900, Khurram wrote:


Nobu Nakada