[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Wes Gamble

3/21/2006 10:29:00 PM

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

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


8 Answers

Daniel Harple

3/21/2006 10:37:00 PM

0


On Mar 21, 2006, at 11:28 PM, Wes Gamble 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?
>
> Thanks,
> Wes

Use printf/sprintf/%

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

-- Daniel


Mark Volkmann

3/21/2006 10:40:00 PM

0

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.


Wes Gamble

3/21/2006 10:44:00 PM

0

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


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


Daniel Harple

3/21/2006 11:13:00 PM

0

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


Wes Gamble

3/21/2006 11:21:00 PM

0

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


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


Wes Gamble

3/21/2006 11:28:00 PM

0

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


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


Mike Stok

3/21/2006 11:28:00 PM

0


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

>
> 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
>
>
> --
> Posted via http://www.ruby-....
>
>

--

Mike Stok <mike@stok.ca>
http://www.stok...

The "`Stok' disclaimers" apply.






Keith Lazuka

3/22/2006 2:38:00 AM

0

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

-keith