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