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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-03-21 23:28
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
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-03-21 23:37
(Received via mailing list)
On Mar 21, 2006, at 11:28 PM, Wes Gamble wrote:

> Wes
Use printf/sprintf/%

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

-- Daniel
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-03-21 23:40
(Received via mailing list)
On 3/21/06, Wes Gamble <weyus@att.net> 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.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-03-21 23:43
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 Harple wrote:
> On Mar 21, 2006, at 11:28 PM, Wes Gamble wrote:
>
>> Wes
> Use printf/sprintf/%
>
> Ex:
> puts "%#x" % 100 -> 0x64
>
> -- Daniel
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-03-22 00:13
(Received via mailing list)
On Mar 21, 2006, at 11:43 PM, Wes Gamble 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
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-03-22 00:20
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 Harple wrote:
> On Mar 21, 2006, at 11:43 PM, Wes Gamble 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
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-03-22 00:27
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

Wes Gamble 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 Harple wrote:
>> On Mar 21, 2006, at 11:43 PM, Wes Gamble 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
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2006-03-22 00:28
(Received via mailing list)
On 21-Mar-06, at 6:20 PM, Wes Gamble 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 Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
45d351f2e88b3df1ac3379124a91ab89?d=identicon&s=25 Keith Lazuka (Guest)
on 2006-03-22 03:37
(Received via mailing list)
To print a hexdump of the string to stdout, check out:
http://www.unixgods.org/~tilo/Ruby/hexdump.html

-keith
This topic is locked and can not be replied to.