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


#1

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


#2

On Mar 21, 2006, at 11:28 PM, Wes G. wrote:

Wes
Use printf/sprintf/%

Ex:
puts “%#x” % 100 -> 0x64

– Daniel


#3

On 3/21/06, Wes G. removed_email_address@domain.invalid wrote:

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?

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.


#4

Daniel,

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

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

wg

Daniel H. wrote:

On Mar 21, 2006, at 11:28 PM, Wes G. wrote:

Wes
Use printf/sprintf/%

Ex:
puts “%#x” % 100 -> 0x64

– Daniel


#5

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

Daniel H. wrote:

On Mar 21, 2006, at 11:43 PM, Wes G. wrote:

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


#6

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.

:slight_smile:

WG

Wes G. wrote:

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

Daniel H. wrote:

On Mar 21, 2006, at 11:43 PM, Wes G. wrote:

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


#7

On 21-Mar-06, at 6:20 PM, Wes G. wrote:

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

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


Posted via http://www.ruby-forum.com/.

Mike S. removed_email_address@domain.invalid
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.


#8

To print a hexdump of the string to stdout, check out:
http://www.unixgods.org/~tilo/Ruby/hexdump.html

-keith


#9

On Mar 21, 2006, at 11:43 PM, Wes G. wrote:

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