Newbie: How do I get hex formatted bytes from a file?

W

Wes Gamble

I want to do:

File.open(path_to_file, "r") do |f|
magic_number = f.read(8)
$stderr.print "Magic number is: #{magic_number}\n"
end

but I want to print the bytes as HEX values - how do I do that?

Thanks,
Wes
 
D

Daniel Harple

I want to do:

File.open(path_to_file, "r") do |f|
magic_number = f.read(8)
$stderr.print "Magic number is: #{magic_number}\n"
end

but I want to print the bytes as HEX values - how do I do that?

Thanks,
Wes

Use printf/sprintf/%

Ex:
puts "%#x" % 100 -> 0x64

-- Daniel
 
M

Mark Volkmann

I want to do:

File.open(path_to_file, "r") do |f|
magic_number =3D f.read(8)
$stderr.print "Magic number is: #{magic_number}\n"
end

but I want to print the bytes as HEX values - how do I do that?

You can do this with String.unpack. In the PickAxe 2 book, see table
27.14 "Directives for String#unpack". In my copy, it's on page 624.
 
W

Wes Gamble

Daniel,

Not to be obtuse, but what is the # sign for in the specification?

And what is the mod 100 -> 0x64 business about?

wg
 
D

Daniel Harple

Daniel,

Not to be obtuse, but what is the # sign for in the specification?

And what is the mod 100 -> 0x64 business about?

wg

Sorry, I misread your question. What you need is String#unpack. Do a
'ri String#upack'. String#% is the format method. It's function is
the same as printf. See Kernel#sprintf for a list of format options
(including #).

-- Daniel
 
W

Wes Gamble

Why do I have to read my bytes into a string and then convert them back?

Is there any way to read an array of bytes from my file?

WG
 
W

Wes Gamble

Answer:

puts "Magic number is: #{magic_number.unpack('H*')}"

Thanks for all the help.

Argh!

I sure wish there were a get_bytes() method on the File and/or IO
objects.

:)

WG
 
M

Mike Stok

Why do I have to read my bytes into a string and then convert them
back?

Is there any way to read an array of bytes from my file?

You can view a String as an array of character values:

---------------------------------------------------------- Class: String
A +String+ object holds and manipulates an arbitrary sequence of
bytes, typically representing characters. String objects may be
created using +String::new+ or as literals.


ratdog:~/tmp mike$ irb
irb(main):001:0> f = File.open('try.rb', 'r')
=> #<File:try.rb>
irb(main):002:0> s = f.read(32)
=> "#!/usr/bin/env ruby\n\nBASENAMES ="
irb(main):003:0> s[0]
=> 35
irb(main):004:0> s[0].chr
=> "#"
irb(main):005:0> '0x%02x' % s[0]
=> "0x23"
irb(main):006:0> s.unpack('H*')
=> ["23212f7573722f62696e2f656e7620727562790a0a424153454e414d4553203d"]

Mike

--

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top