[Ironruby-core] byte[] to ruby string

Tomas Matousek Tomas.Matousek at microsoft.com
Thu Oct 22 21:56:38 EDT 2009

In this particular case, I would recommend to use .NET API:

>>> include System::IO
=> Object
>>> stream = FileStream.new("c:\\temp\\a.txt", FileMode.Open, FileAccess.Read)
=> System.IO.FileStream
>>> "".Append(stream, stream.Length)
=> "hello"


-----Original Message-----
From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Håkon Clausen
Sent: Thursday, October 22, 2009 2:13 PM
To: ironruby-core at rubyforge.org
Subject: [Ironruby-core] byte[] to ruby string


I'm struggling with converting a byte array into a ruby binary string.
Does anybody know how to make something like this pass?

require 'test/unit'
include System::IO
class Binstring < Test::Unit::TestCase

   def test_binstring_bytea
     fileName = 'flowers.jpg'

     #Ruby way
     data1 = File.read(fileName)

     fs = FileStream.new(fileName, FileMode.Open, FileAccess.Read);
     br = BinaryReader.new(fs);
     numBytes = FileInfo.new(fileName).Length;
     buff = br.ReadBytes(numBytes);
     data2 = System::Text::ASCIIEncoding.new.GetString(buff).to_s

     assert data1 == data2




Håkon Clausen
Nosyko AS

"Nobody will ever need more than 640k RAM!" - Bill Gates, 1981.
Ironruby-core mailing list
Ironruby-core at rubyforge.org<mailto:Ironruby-core at rubyforge.org>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20091023/0f5e8691/attachment.html>

More information about the Ironruby-core mailing list