# How to save string to a file as UTF-8

**URL:** <https://www.ruby-forum.com/t/how-to-save-string-to-a-file-as-utf-8/172931>\
**Category:** Ruby\
**Created:** [July 30, 2009, 4:29am UTC](https://www.ruby-forum.com/t/how-to-save-string-to-a-file-as-utf-8/172931 "2009-07-30T04:29:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4bbf92/32.png) [@bali](https://www.ruby-forum.com/u/bali)\
**Post date:** [July 30, 2009, 4:29am UTC](https://www.ruby-forum.com/t/how-to-save-string-to-a-file-as-utf-8/172931/1 "2009-07-30T04:29:07Z")

</div>

## Below code:

s1 = “good”

## File.new(“test.xml”, “w+”) \<\< s1

It always give a file ANSI encoded. How can I get it UTF-8 encoded?

Thanks.

---

<div class="post-metadata">

**Author:** ![bali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4bbf92/32.png) [@bali](https://www.ruby-forum.com/u/bali)\
**Post date:** [July 30, 2009, 5:43am UTC](https://www.ruby-forum.com/t/how-to-save-string-to-a-file-as-utf-8/172931/2 "2009-07-30T05:43:14Z")

</div>

Bali Bali wrote:

> 

If your ruby version \< “1.9”  
$KCODE = “UTF8”  
else  
#!/usr/bin/env ruby

# encoding: utf-8 \<— this line

end

(NOTES : above code is not legal ruby code)

---

<div class="post-metadata">

**Author:** ![bali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4bbf92/32.png) [@bali](https://www.ruby-forum.com/u/bali)\
**Post date:** [July 30, 2009, 6:11am UTC](https://www.ruby-forum.com/t/how-to-save-string-to-a-file-as-utf-8/172931/3 "2009-07-30T06:11:41Z")

</div>

pierr wrote:

> Bali Bali wrote:
> 
> > 
> 
> If your ruby version \< “1.9”  
> $KCODE = “UTF8”  
> else  
> #!/usr/bin/env ruby
> 
> # encoding: utf-8 \<— this line
> 
> end
> 
> (NOTES : above code is not legal ruby code)

pierr, Thanks! But looks like it is not working for me.

## Code v2.0

$KCODE = “UTF-8”

s1 = “good”

## File.new(“test.xml”, “w+”) \<\< s1

It still creates a ANSI file. My Ruby version:  
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

It is windows release which should not be related to the issue.

Any idea?

---

<div class="post-metadata">

**Author:** ![bali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4bbf92/32.png) [@bali](https://www.ruby-forum.com/u/bali)\
**Post date:** [July 30, 2009, 3:01pm UTC](https://www.ruby-forum.com/t/how-to-save-string-to-a-file-as-utf-8/172931/4 "2009-07-30T15:01:04Z")

</div>

You can try

```
require 'iconv'
myEncoding = 'iso-8859-15' # or 'CP1252' or what you have
s1 = 

```

‘ü’ s1 = Iconv.new(‘utf-8’, myEncoding ).iconv(s1)

Maybe, you will need the “Byte-order mark”. That are 3 special Bytes  
at the very beginning of your utf8-file:  
[wikipedia.org](http://wikipedia.org) \> utf-8 \> “Byte-order mark”

---

<div class="post-metadata">

**Author:** ![bali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4bbf92/32.png) [@bali](https://www.ruby-forum.com/u/bali)\
**Post date:** [July 30, 2009, 4:15pm UTC](https://www.ruby-forum.com/t/how-to-save-string-to-a-file-as-utf-8/172931/5 "2009-07-30T16:15:50Z")

</div>

Bali Bali wrote:

> ## Below code:
> 
> s1 = “good”
> 
> ## File.new(“test.xml”, “w+”) \<\< s1
> 
> It always give a file ANSI encoded. How can I get it UTF-8 encoded?

What do you mean by “ANSI encoded”? Do you mean “ASCII”?

The text “good” is exactly the same in both ASCII and UTF-8, so how do  
you know it is one and not the other? Can you give a different example  
which shows the problem?

Use “hexdump -C test.xml” to show the exact content of the file, byte by  
byte.
