extracting hex keys from file

B

Ben Aroia

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
 
R

Robert Klemme

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.html#M002305

robert
 
B

Ben Aroia

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)
 
R

Robert Klemme

Please do not top post.

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
 
J

Jari Williamsson

Convert the integer to a string using base 16:

f.getc.to_s(16)


Best regards,

Jari Williamsson
 
B

Ben Aroia

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

Jari Williamsson

Ben said:
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
 
S

Siep Korteling

Ben said:
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
 
R

Robert Klemme

2008/2/28 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top