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-forum.com/.
>
>

-- 

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

The "`Stok' disclaimers" apply.