# Writing text files with an explicit encoding (UTF-16LE)

**URL:** <https://www.ruby-forum.com/t/writing-text-files-with-an-explicit-encoding-utf-16le/127698>\
**Category:** Ruby\
**Created:** [January 31, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/writing-text-files-with-an-explicit-encoding-utf-16le/127698 "2008-01-31T14:46:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [January 31, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/writing-text-files-with-an-explicit-encoding-utf-16le/127698/1 "2008-01-31T14:46:09Z")

</div>

Hey all,

I cant seem to find any documentation on how to write a text file to the  
filesystem with an explicit encoding type? Can someone point me in the  
right direction? Its a simple text file, but it _needs_ to be UTF-16.

Cheers

Tim

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [January 31, 2008, 3:12pm UTC](https://www.ruby-forum.com/t/writing-text-files-with-an-explicit-encoding-utf-16le/127698/2 "2008-01-31T15:12:09Z")

</div>

Tim P. wrote:

> Hey all,
> 
> I cant seem to find any documentation on how to write a text file to the  
> filesystem with an explicit encoding type? Can someone point me in the  
> right direction? Its a simple text file, but it _needs_ to be UTF-16.
> 
> Cheers
> 
> Tim

require ‘iconv’

converter = Iconv.new(“UTF-16”, “ISO-8859-15”)  
utf\_16\_str = converter.iconv(‘hello world’)  
p utf\_16\_str

–output:–  
“\376\377\000h\000e\000l\000l\000o\000 \000w\000o\000r\000l\000d”

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [January 31, 2008, 4:11pm UTC](https://www.ruby-forum.com/t/writing-text-files-with-an-explicit-encoding-utf-16le/127698/3 "2008-01-31T16:11:38Z")

</div>

7stud – wrote:

> require ‘iconv’
> 
> converter = Iconv.new(“UTF-16”, “ISO-8859-15”)  
> utf\_16\_str = converter.iconv(‘hello world’)  
> p utf\_16\_str
> 
> –output:–  
> “\376\377\000h\000e\000l\000l\000o\000 \000w\000o\000r\000l\000d”

Whoops. I overlooked the ‘LE’ in your title:

require ‘iconv’

converter = Iconv.new(“UTF-16LE”, “ISO-8859-15”)  
utf\_16\_str = converter.iconv(‘hello world’)  
p utf\_16\_str

–output:–  
“h\000e\000l\000l\000o\000 \000w\000o\000r\000l\000d\000”

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [January 31, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/writing-text-files-with-an-explicit-encoding-utf-16le/127698/4 "2008-01-31T16:20:52Z")

</div>

Yup - it works 🙂

Thanks!

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [January 31, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/writing-text-files-with-an-explicit-encoding-utf-16le/127698/5 "2008-01-31T16:16:15Z")

</div>

7stud – wrote:

> require ‘iconv’
> 
> converter = Iconv.new(“UTF-16”, “ISO-8859-15”)  
> utf\_16\_str = converter.iconv(‘hello world’)  
> p utf\_16\_str
> 
> –output:–  
> “\376\377\000h\000e\000l\000l\000o\000 \000w\000o\000r\000l\000d”

So presumably something like the below would work?

converter = Iconv.new(“UTF-16”, “ISO-8859-15”)  
utf\_16\_str = converter.iconv(‘hello world’)

File.open(‘example.txt’, ‘w’) do |f|  
f.puts utf\_16\_str  
end
