# 1.9, C extension vs encoding

**URL:** <https://www.ruby-forum.com/t/1-9-c-extension-vs-encoding/146402>\
**Category:** Ruby\
**Created:** [August 20, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/1-9-c-extension-vs-encoding/146402 "2008-08-20T17:23:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor\_SZverokS\_She](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@Victor\_SZverokS\_She](https://www.ruby-forum.com/u/Victor_SZverokS_She)\
**Post date:** [August 20, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/1-9-c-extension-vs-encoding/146402/1 "2008-08-20T17:23:18Z")

</div>

Hello all.

Ruby 1.9.  
When I create some strings with Russian letters in C extensions, they  
have

str.encoding #=\> #Encoding:ASCII-8BIT

while being created in Ruby, they have proper UTF-8. Where is I’m idiot?

Thanks.

V.

---

<div class="post-metadata">

**Author:** ![Victor\_SZverokS\_She](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@Victor\_SZverokS\_She](https://www.ruby-forum.com/u/Victor_SZverokS_She)\
**Post date:** [August 23, 2008, 2:03am UTC](https://www.ruby-forum.com/t/1-9-c-extension-vs-encoding/146402/2 "2008-08-23T02:03:14Z")

</div>

Hi,

On Thu, Aug 21, 2008 at 12:18 AM, Victor ‘Zverok’ Shepelev  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello all.
> 
> Ruby 1.9.  
> When I create some strings with Russian letters in C extensions, they have
> 
> str.encoding #=\> #Encoding:ASCII-8BIT
> 
> while being created in Ruby, they have proper UTF-8. Where is I’m idiot?

rb\_str\_new() creates a ASCII-8BIT string in Ruby 1.9.  
You need to use rb\_enc\_str\_new() with proper rb\_encoding.  
If your extensions always make UTF8 strings, use rb\_utf8\_encoding().  
If the encoding depends on locale, use rb\_locale\_encoding().
