From: Brian Candler on
Praveen wrote:
> I wanted to know if there is any function in the C extension
> (Ruby-1.9) that can be used to convert the encoding of the string to
> the encoding format specified by the user (in his environment or by
> setting #encoding: at the beginning of .rb file).

Those are two different things.

The encoding guessed from the environment is Encoding.default_external
(or Encoding.find("external")). So you can do:

str2 = str1.encode("external")

The encoding specified in the #encoding line is called the source
encoding, and it's the encoding which string literals like "abc"
(usually) get automatically.

Maybe it would be helpful first to review the concepts in 'pure ruby',
then map them to C. There are some details at:

http://github.com/candlerb/string19/blob/master/string19.rb

http://blog.grayproductions.net/articles/ruby_19s_string (plus see other
articles linked from the table of contents)

Warning: it's a large and complex topic.

> I did find 2 function namely rb_str_export and rb_str_export_locale.
> Not sure which one will convert the strings rightly to the format
> which the user is set.

I suggest you start by calling the ruby-level functions, like
String#encode. If there is a particular need to call the underlying C
functions directly then you can look at the ruby source code for
String#encode and see what it calls.
--
Posted via http://www.ruby-forum.com/.