# How to use rb\_enc\_str\_new() to create a String with UTF-8 encoding?

**URL:** <https://www.ruby-forum.com/t/how-to-use-rb-enc-str-new-to-create-a-string-with-utf-8-encoding/179720>\
**Category:** Ruby\
**Created:** [December 2, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/how-to-use-rb-enc-str-new-to-create-a-string-with-utf-8-encoding/179720 "2009-12-02T16:17:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [December 2, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/how-to-use-rb-enc-str-new-to-create-a-string-with-utf-8-encoding/179720/1 "2009-12-02T16:17:47Z")

</div>

Hi, when I create a Ruby String from a C extension by using  
“rb\_str\_new(s,  
len)” I get a String with US-ASCII encoding.

I don’t want to call later String#force\_encoding(:“UTF-8”) but instead  
use the  
rb\_enc\_str\_new() function in string.c:

VALUE  
rb\_enc\_str\_new(const char \*ptr, long len, rb\_encoding \*enc)  
{  
VALUE str = rb\_str\_new(ptr, len);  
rb\_enc\_associate(str, enc);  
return str;  
}

But I have no idea on how to set ‘enc’ parameter to be “UTF-8”.  
How should I fill the third ‘enc’ argument?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [December 2, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/how-to-use-rb-enc-str-new-to-create-a-string-with-utf-8-encoding/179720/2 "2009-12-02T17:45:28Z")

</div>

IÃ±aki Baz C. wrote:

> VALUE  
> rb\_enc\_str\_new(const char \*ptr, long len, rb\_encoding \*enc)  
> {  
> VALUE str = rb\_str\_new(ptr, len);  
> rb\_enc\_associate(str, enc);  
> return str;  
> }
> 
> But I have no idea on how to set ‘enc’ parameter to be “UTF-8”.  
> How should I fill the third ‘enc’ argument?

I’d say give it a pointer to an rb\_encoding object.

Have a look in encoding.c, this particular function might be useful:

rb\_encoding \*  
rb\_enc\_find(const char \*name)  
{  
int idx = rb\_enc\_find\_index(name);  
if (idx \< 0) idx = 0;  
return rb\_enc\_from\_index(idx);  
}

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [December 2, 2009, 6:04pm UTC](https://www.ruby-forum.com/t/how-to-use-rb-enc-str-new-to-create-a-string-with-utf-8-encoding/179720/3 "2009-12-02T18:04:55Z")

</div>

El MiÃ©rcoles, 2 de Diciembre de 2009, Brian C. escribiÃ³:

> > How should I fill the third ‘enc’ argument?  
> > return rb\_enc\_from\_index(idx);  
> > }

Humm, it involves allocating memory for the rb\_encoding object and so…  
not  
so trivial as I desired 🙂  
But that’s the way. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [December 2, 2009, 6:59pm UTC](https://www.ruby-forum.com/t/how-to-use-rb-enc-str-new-to-create-a-string-with-utf-8-encoding/179720/4 "2009-12-02T18:59:15Z")

</div>

IÃ±aki Baz C. wrote:

> El MiÃ©rcoles, 2 de Diciembre de 2009, Brian C. escribiÃ³:
> 
> > > How should I fill the third ‘enc’ argument?  
> > > return rb\_enc\_from\_index(idx);  
> > > }
> 
> Humm, it involves allocating memory for the rb\_encoding object

Why? AFAICS, you can just pass a pointer to an existing encoding object.  
They are not mutated.

There are other examples, e.g. from io.c

#ifdef \_WIN32  
if (utf16 == (rb\_encoding _)-1) {  
utf16 = rb\_enc\_find(“UTF-16LE”);  
if (utf16 == rb\_ascii8bit\_encoding())  
utf16 = NULL;  
}  
if (utf16) {  
VALUE wfname = rb\_str\_encode(fname, rb\_enc\_from\_encoding(utf16),  
0,  
Qnil);  
rb\_enc\_str\_buf\_cat(wfname, “”, 1, utf16); /_ workaround \*/  
data.fname = RSTRING\_PTR(wfname);  
data.wchar = 1;  
}  
else {  
data.wchar = 0;  
}  
#endif

It looks like rb\_enc\_from\_encoding() takes a pointer to the rb\_encoding  
object returned from rb\_enc\_find, and turns it into a VALUE

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [December 2, 2009, 11:03pm UTC](https://www.ruby-forum.com/t/how-to-use-rb-enc-str-new-to-create-a-string-with-utf-8-encoding/179720/5 "2009-12-02T23:03:25Z")

</div>

El MiÃ©rcoles, 2 de Diciembre de 2009, Brian C. escribiÃ³:

> They are not mutated.  
> VALUE wfname = rb\_str\_encode(fname, rb\_enc\_from\_encoding(utf16),
> 
> It looks like rb\_enc\_from\_encoding() takes a pointer to the rb\_encoding  
> object returned from rb\_enc\_find, and turns it into a VALUE

Ok, so the rb\_encoding objects already exist and I just must use a point  
to  
it.  
Thanks a lot.
