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

T

Tim Perrett

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
 
7

7stud --

Tim said:
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"
 
7

7stud --

7stud said:
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"
 
T

Tim Perrett

7stud said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top