# Bytes, strings and encodings

**URL:** <https://www.ruby-forum.com/t/bytes-strings-and-encodings/90627>\
**Category:** Rails\
**Created:** [March 30, 2007, 2:02pm UTC](https://www.ruby-forum.com/t/bytes-strings-and-encodings/90627 "2007-03-30T14:02:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@mare](https://www.ruby-forum.com/u/mare)\
**Post date:** [March 30, 2007, 2:02pm UTC](https://www.ruby-forum.com/t/bytes-strings-and-encodings/90627/1 "2007-03-30T14:02:04Z")

</div>

Hi all

I’m creating a Ruby client for some web service and need to send a file  
to that service. Since I’m a newbie, I have problems translating this  
Java code into Ruby:

byte[] origFile = FileUtils.readFileToByteArray(new File(filePath));  
String content = encodeBase64(new String(origFile, “ISO-8859-1”)));

So, I need to get a file as an array of bytes and then transform it into  
ISO-8859-1 encoded string that should be Base64 encoded afterwards.

I tried the following:

origFile = “”  
open(filePath) { |f| origFile += f.gets}  
content = Base64.encode64(origFile)

hoping that, eventually, everything will somehow work ok 🙂  
Of course it didn’t… (the contents were similar, but not the same)

Help!

---

<div class="post-metadata">

**Author:** ![mare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@mare](https://www.ruby-forum.com/u/mare)\
**Post date:** [March 30, 2007, 3:21pm UTC](https://www.ruby-forum.com/t/bytes-strings-and-encodings/90627/2 "2007-03-30T15:21:32Z")

</div>

On Mar 30, 2007, at 8:02 AM, mare wrote:

> hoping that, eventually, everything will somehow work ok 🙂  
> Of course it didn’t… (the contents were similar, but not the same)
> 
> Help!

You’re close, try this:

content = Base64.encode64(File.read(filePath))

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@mare](https://www.ruby-forum.com/u/mare)\
**Post date:** [April 2, 2007, 11:41am UTC](https://www.ruby-forum.com/t/bytes-strings-and-encodings/90627/3 "2007-04-02T11:41:35Z")

</div>

Rob B. wrote:

> On Mar 30, 2007, at 8:02 AM, mare wrote:
> 
> You’re close, try this:
> 
> content = Base64.encode64(File.read(filePath))

tried, but the results from Java and this code do not match… Any ideas  
why?

---

<div class="post-metadata">

**Author:** ![mare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@mare](https://www.ruby-forum.com/u/mare)\
**Post date:** [April 2, 2007, 3:52pm UTC](https://www.ruby-forum.com/t/bytes-strings-and-encodings/90627/4 "2007-04-02T15:52:36Z")

</div>

On Apr 2, 2007, at 5:41 AM, Marko Marjanovic wrote:

> Rob B. wrote:
> 
> > On Mar 30, 2007, at 8:02 AM, mare wrote:
> > 
> > You’re close, try this:
> > 
> > content = Base64.encode64(File.read(filePath))
> 
> tried, but the results from Java and this code do not match… Any  
> ideas  
> why?

If you’re on Windoze, perhaps:  
f = File.open(filePath, ‘rb’)  
encoded = f.read  
f.close  
content = Base64.encode64(encoded)

Or take the Base64 out of the picture and make sure that you get the  
same result on a plain file. If you have a difference in the length  
of the output by 4/3 of the number of lines in the original, it’s  
very likely a line ending difference.

Other than that, no one is going to be able to help without more  
information. Perhaps create a small file and post the result if you  
can’t figure it out from there.

$ irb

> > require ‘base64’  
> > =\> true  
> > filename = “sample.txt”  
> > =\> “sample.txt”  
> > File.open(filename, “wb”) { |f| f.write “hello, world\n” }  
> > =\> 13  
> > puts Base64.encode64(File.read(filename))  
> > aGVsbG8sIHdvcmxkCg==  
> > =\> nil  
> > puts Base64.decode64(“aGVsbG8sIHdvcmxkCg==”)  
> > hello, world  
> > =\> nil  
> > p Base64.decode64(“aGVsbG8sIHdvcmxkCg==”)  
> > “hello, world\n”  
> > =\> nil

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@mare](https://www.ruby-forum.com/u/mare)\
**Post date:** [April 3, 2007, 11:31am UTC](https://www.ruby-forum.com/t/bytes-strings-and-encodings/90627/5 "2007-04-03T11:31:47Z")

</div>

> If you’re on Windoze, perhaps:  
> f = File.open(filePath, ‘rb’)  
> encoded = f.read  
> f.close  
> content = Base64.encode64(encoded)

I tried the code you suggested and compared the results with the Java’s  
output. The following happened:

1. Performed on a small csv file, both Java and Ruby gave the same  
output (the file was cp1252 encoded with \r\n line endings).
2. Performed on the same file saved as xls (Excel) spreadsheet, Java  
gave cp1252 encoded output with \r\n line endings and Ruby gave utf-8  
encoded output with \n line endings.

I read these attributes from JEdit. Both Java and Ruby codes were saved  
and run from Eclipse.  
Any ideas how it could be fixed? Should I provide more (and what)  
information?

Thanks

---

<div class="post-metadata">

**Author:** ![mare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@mare](https://www.ruby-forum.com/u/mare)\
**Post date:** [April 4, 2007, 11:25am UTC](https://www.ruby-forum.com/t/bytes-strings-and-encodings/90627/6 "2007-04-04T11:25:54Z")

</div>

Anyone?
