Binary reading

a=File.open("c:\\1.txt")
a.binmode
print a.read(1)

it prints the char. how can i get and play with the bits and bytes of the file?

ruby talk wrote:

a=File.open("c:\\1.txt")
a.binmode
print a.read(1)

it prints the char. how can i get and play with the bits and bytes of the file?

a=File.open("c:\\1.txt")
a.each_byte{ |byte|
  #play with each character/byte here
}

Zach

hmm... i really wanted the binary. is there a to_b?

···

On Tue, 23 Nov 2004 13:11:50 +0900, Zach Dennis <zdennis@mktec.com> wrote:

ruby talk wrote:

> a=File.open("c:\\1.txt")
> a.binmode
> print a.read(1)
>
> it prints the char. how can i get and play with the bits and bytes of the file?

a=File.open("c:\\1.txt")
a.each_byte{ |byte|
        #play with each character/byte here
}

Zach

Are you sure you want the binary? You can use the bitwise |, ~, and &
operators to manipulate the bits of the byte.

···

On Tue, 23 Nov 2004 13:22:56 +0900, ruby talk <rubytalk@gmail.com> wrote:

hmm... i really wanted the binary. is there a to_b?

On Tue, 23 Nov 2004 13:11:50 +0900, Zach Dennis <zdennis@mktec.com> wrote:
> ruby talk wrote:
>
>
>
> > a=File.open("c:\\1.txt")
> > a.binmode
> > print a.read(1)
> >
> > it prints the char. how can i get and play with the bits and bytes of the file?
>
> a=File.open("c:\\1.txt")
> a.each_byte{ |byte|
> #play with each character/byte here
> }
>
>
> Zach
>
>

I think you want to look at String#unpack.

···

On 22 Nov 2004, at 20:22, ruby talk wrote:

hmm... i really wanted the binary. is there a to_b?

I looked at the unpack example
a=File.open("c:\\1.txt")
a.each_byte { |byte|
      print byte.to_s.unpack('b8'),' ',byte,' '
}

But i get this for output
01101100 65 01101100 66
i know A!=B
and b* instead of b8 gives me
0110110010101100 65 0110110001101100 66

Becker

···

On Tue, 23 Nov 2004 14:59:44 +0900, Eric Hodel <drbrain@segment7.net> wrote:

On 22 Nov 2004, at 20:22, ruby talk wrote:

> hmm... i really wanted the binary. is there a to_b?

I think you want to look at String#unpack.

What about (written as if it were a patch):

- a=File.open("c:\\1.txt")
+ a=File.open("c:\\1.txt", 'rb')
   a.each_byte { |byte|
- print byte.to_s.unpack('b8'),' ',byte,' '
+ print byte.chr.unpack('b8'),' ',byte,' '
   }

         Hugh

"ruby talk" <rubytalk@gmail.com> schrieb im Newsbeitrag
news:25094bf7041123040921b14b34@mail.gmail.com...

I looked at the unpack example
a=File.open("c:\\1.txt")
a.each_byte { |byte|
      print byte.to_s.unpack('b8'),' ',byte,' '
}

But i get this for output
01101100 65 01101100 66
i know A!=B
and b* instead of b8 gives me
0110110010101100 65 0110110001101100 66

File.open("buildnumber","rb") {|a| a.each_byte {|b| printf("%s %4d

%08b\n", b.chr,b,b) } }
V 86 01010110
e 101 01100101
r 114 01110010
s 115 01110011
i 105 01101001
o 111 01101111
n 110 01101110
    32 00100000
4 52 00110100
.. 46 00101110
4 52 00110100
    32 00100000
B 66 01000010
u 117 01110101
i 105 01101001
l 108 01101100
d 100 01100100
    32 00100000
1 49 00110001
6 54 00110110
2 50 00110010
=> #<File:buildnumber (closed)>

Regards

    robert

10000010 65 01000010 66

the output is still wrong
I also tried adding a.binmode no luck.
Will i also have such a problem writing a binary file?
Becker

···

On Tue, 23 Nov 2004 21:39:42 +0900, Hugh Sasse Staff Elec Eng <hgs@dmu.ac.uk> wrote:

What about (written as if it were a patch):

- a=File.open("c:\\1.txt")
+ a=File.open("c:\\1.txt", 'rb')
   a.each_byte { |byte|
- print byte.to_s.unpack('b8'),' ',byte,' '
+ print byte.chr.unpack('b8'),' ',byte,' '
   }

         Hugh

It's printing the LSB first. unpack("B8") would do what you expect.
Alternatively num.to_s(2) or "%08b" % num .

···

On Wed, Nov 24, 2004 at 12:48:22AM +0900, ruby talk wrote:

10000010 65 01000010 66

the output is still wrong

--
Hassle-free packages for Ruby?
RPA is available from http://www.rubyarchive.org/