[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

extracting hex keys from file

Ben Aroia

2/27/2008 4:08:00 AM

Hello. I have a file that contains several values that I would like to
read in as hex values (e.x. 3F 5A 6B 43 0E 2F AE...) I have some partial
code:

def getHash(file_in)
f = File.new(file_in, "r")
f.each_byte {|x| print x.to_s+"\n"}
#need something here
end

I would like to extract the hex values from a known offset for a
specific length, and I'm quite sure I can do that by accelorating the
pointer a bit with a simple for loop. I come from a Java background and
new to ruby so any help would be great. Thanks
--
Posted via http://www.ruby-....

10 Answers

Robert Klemme

2/27/2008 8:03:00 AM

0

On 27.02.2008 05:08, Ben Aroia wrote:
> Hello. I have a file that contains several values that I would like to
> read in as hex values (e.x. 3F 5A 6B 43 0E 2F AE...) I have some partial
> code:
>
> def getHash(file_in)
> f = File.new(file_in, "r")
> f.each_byte {|x| print x.to_s+"\n"}
> #need something here
> end
>
> I would like to extract the hex values from a known offset for a
> specific length, and I'm quite sure I can do that by accelorating the
> pointer a bit with a simple for loop. I come from a Java background and
> new to ruby so any help would be great. Thanks

http://www.ruby-doc.org/core/classes/IO.ht...

robert

Ben Aroia

2/27/2008 9:35:00 PM

0

I've read that. I'm looking for an f.gethex (returns a string with "F4"
or something) I could even deal with converting the f.getc value to a
hex value (in a string format) I'm considering writing a giant if
statement (but my fingers are starting to hurt)

Robert Klemme wrote:
> On 27.02.2008 05:08, Ben Aroia wrote:
>> I would like to extract the hex values from a known offset for a
>> specific length, and I'm quite sure I can do that by accelorating the
>> pointer a bit with a simple for loop. I come from a Java background and
>> new to ruby so any help would be great. Thanks
>
> http://www.ruby-doc.org/core/classes/IO.ht...
>
> robert
--
Posted via http://www.ruby-....

Robert Klemme

2/27/2008 10:15:00 PM

0


Please do not top post.

On 27.02.2008 22:34, Ben Aroia wrote:
> I've read that.

So what did you mean by "from a known offset" and "accelerating the
pointer a bit with a simple loop"?

> I'm looking for an f.gethex (returns a string with "F4"
> or something) I could even deal with converting the f.getc value to a
> hex value (in a string format) I'm considering writing a giant if
> statement (but my fingers are starting to hurt)

What do you need that for? Are you looking for
http://ruby-doc.org/core/classes/Fixnum.ht... ?

Cheers

robert

Jari Williamsson

2/27/2008 10:16:00 PM

0

Convert the integer to a string using base 16:

f.getc.to_s(16)


Best regards,

Jari Williamsson


Ben Aroia wrote:
> I've read that. I'm looking for an f.gethex (returns a string with "F4"
> or something) I could even deal with converting the f.getc value to a
> hex value (in a string format) I'm considering writing a giant if
> statement (but my fingers are starting to hurt)
>
> Robert Klemme wrote:
>> On 27.02.2008 05:08, Ben Aroia wrote:
>>> I would like to extract the hex values from a known offset for a
>>> specific length, and I'm quite sure I can do that by accelorating the
>>> pointer a bit with a simple for loop. I come from a Java background and
>>> new to ruby so any help would be great. Thanks
>> http://www.ruby-doc.org/core/classes/IO.ht...
>>
>> robert

Robert Klemme

2/27/2008 10:17:00 PM

0

On 27.02.2008 23:15, Robert Klemme wrote:
>
> Please do not top post.
>
> On 27.02.2008 22:34, Ben Aroia wrote:
>> I've read that.
>
> So what did you mean by "from a known offset" and "accelerating the
> pointer a bit with a simple loop"?
>
>> I'm looking for an f.gethex (returns a string with "F4" or something)
>> I could even deal with converting the f.getc value to a hex value (in
>> a string format) I'm considering writing a giant if statement (but my
>> fingers are starting to hurt)
>
> What do you need that for? Are you looking for
> http://ruby-doc.org/core/classes/Fixnum.ht... ?

PS: there's also String#unpack.


Ben Aroia

2/27/2008 10:36:00 PM

0

> PS: there's also String#unpack.
That proved very useful. Thanks.

I meant that the hex string I was gunning for was, say 110 bytes into
the file, and I wanted everything between 110 and 200 to be read in in
hex. I meant I'd do something like
f = File.open("name","r")
for i in 0..109 do
f.getc
end

to bump the pointer forward 110 bytes.
--
Posted via http://www.ruby-....

Jari Williamsson

2/27/2008 11:05:00 PM

0

Ben Aroia wrote:
>> PS: there's also String#unpack.
> That proved very useful. Thanks.
>
> I meant that the hex string I was gunning for was, say 110 bytes into
> the file, and I wanted everything between 110 and 200 to be read in in
> hex. I meant I'd do something like
> f = File.open("name","r")
> for i in 0..109 do
> f.getc
> end
>
> to bump the pointer forward 110 bytes.

It's more Rubyish to use internal iterators rather than external, for
example 0.upto(109) or (0..109).each or 110.times or...

But use IO#pos= instead of a loop


Best regards,

Jari Williamsson

Siep Korteling

2/27/2008 11:11:00 PM

0

Ben Aroia wrote:

>
> to bump the pointer forward 110 bytes.

Robert Klemme did point this out in his very first post. I did not know
you could do this kind of low-level stuff in Ruby; it's probably going
to speed up some of my scripts.

regards,

Siep
--
Posted via http://www.ruby-....

Ben Aroia

2/27/2008 11:14:00 PM

0

The java background is killing me here. ;) Thanks for pointing out the
IO#pos.
--
Posted via http://www.ruby-....

Robert Klemme

2/28/2008 8:55:00 AM

0

2008/2/28, Ben Aroia <benaroia@gmail.com>:
> The java background is killing me here. ;) Thanks for pointing out the
> IO#pos.

IMHO something else is killing you: you did give only fractions of
your problem description making it hard for people to come up with
suggestions.

Back to your problem: here's a more efficient solution than looping:

def read_hex_io(io, offset, count)
io.seek offset, File::SEEK_SET
bytes = io.read count
bytes.unpack("H*").shift
end

def read_hex(file, offset, count)
File.open(file, "rb") do |io|
read_hex_io io, offset, count
end
end

Note: use the block form of File.open to ensure timely and safe
closing of file descriptors.

Cheers

robert

--
use.inject do |as, often| as.you_can - without end