Extracting hex keys from file

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

On 27.02.2008 05:08, Ben A. 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.html#M002305

robert

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 K. wrote:

On 27.02.2008 05:08, Ben A. 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.html#M002305

robert

Convert the integer to a string using base 16:

f.getc.to_s(16)

Best regards,

Jari W.

Please do not top post.

On 27.02.2008 22:34, Ben A. 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.html#M001069 ?

Cheers

robert

On 27.02.2008 23:15, Robert K. wrote:

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.html#M001069 ?

PS: there’s also String#unpack.

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.

Ben A. wrote:

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 W.

Ben A. wrote:

to bump the pointer forward 110 bytes.

Robert K. 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

The java background is killing me here. :wink: Thanks for pointing out the
IO#pos.

2008/2/28, Ben A. [email protected]:

The java background is killing me here. :wink: 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