[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

binary reading

RubyTalk@gmail.com

11/23/2004 4:05:00 AM

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?


9 Answers

Zach Dennis

11/23/2004 4:12:00 AM

0

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


RubyTalk@gmail.com

11/23/2004 4:23:00 AM

0

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
>
>


Bill Atkins

11/23/2004 5:00:00 AM

0

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
> >
> >
>
>


Eric Hodel

11/23/2004 6:00:00 AM

0

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.



RubyTalk@gmail.com

11/23/2004 12:10:00 PM

0

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.
>
>


Hugh Sasse

11/23/2004 12:40:00 PM

0

Robert Klemme

11/23/2004 12:44:00 PM

0


"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

RubyTalk@gmail.com

11/23/2004 3:48:00 PM

0

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
>
>


Mauricio Fernández

11/23/2004 4:14:00 PM

0

On Wed, Nov 24, 2004 at 12:48:22AM +0900, ruby talk wrote:
> 10000010 65 01000010 66
>
> the output is still wrong

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

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