string to UTF

J

Jean-Baptiste

How to convert a single string into UTF format.

str = "test"
strUTF = str. ?????

Thanks
 
S

Simon Strandgaard

N

nobu.nokada

Hi,

At Sat, 20 Dec 2003 02:56:55 +0900,
Mark said:
Which isn't much of a test since the UTF-8 of "test" is just "test".
This is a better demonstration:

irb(main):004:0> convertor.iconv("¡Hola!")
=> "\302\241Hola!"

The two-byte sequence "\302\241" - that is, a byte whose value is
octal 302 = decimal 194 = hexadecimal C2 followed by a byte whose value is
octal 241 = decimal 161 = hexadecimal A1, is, in fact, the UTF-8 encoding
of the character U+00A1 INVERTED EXCLAMATION MARK.

Only for ISO-8859-1, iconv is not needed.

$ ruby -e 'p "\xa1Hola!".unpack("C*").pack("U*")'
"\302\241Hola!"
 
P

Piergiuliano Bossi

Mark said:
Use the iconv library, which comes with Ruby 1.8.

Is iconv available on Windows 1.8.0 PragProg distribution?

C:\TEMP\x>ruby -v
ruby 1.8.0 (2003-08-04) [i386-mswin32]
You have to know
what encoding you're starting with - ISO-8859-1 (a.k.a Latin-1) or
whatever your local character set is. Note the order of arguments
in Iconv.open: the target character set comes first, then the source
character set.

irb(main):001:0> require 'iconv'
=> true

Not for me:

irb(main):001:0> require 'iconv'
LoadError: No such file to load -- iconv
from (irb):1:in `require'
from (irb):1
irb(main):002:0> VERSION
=> "1.8.0"

Trying your code in a file named 'tryconv.rb':

require 'iconv'
convertor = Iconv.open("UTF-8", "ISO-8859-1")
strUTF = convertor.iconv("test")

I get:

C:\TEMP\x>ruby tryconv.rb
./iconv.rb:2: uninitialized constant Iconv (NameError)
from tryconv.rb:1:in `require'
from tryconv.rb:1

Cheers, Giuliano
 
E

Emmanuel Touzery

Piergiuliano said:
Mark said:
Use the iconv library, which comes with Ruby 1.8.


Is iconv available on Windows 1.8.0 PragProg distribution?

C:\TEMP\x>ruby -v
ruby 1.8.0 (2003-08-04) [i386-mswin32]
You have to know
what encoding you're starting with - ISO-8859-1 (a.k.a Latin-1) or
whatever your local character set is. Note the order of arguments
in Iconv.open: the target character set comes first, then the source
character set.

irb(main):001:0> require 'iconv'
=> true


Not for me:

irb(main):001:0> require 'iconv'
LoadError: No such file to load -- iconv
from (irb):1:in `require'
from (irb):1
irb(main):002:0> VERSION
=> "1.8.0"

Trying your code in a file named 'tryconv.rb':

require 'iconv'
convertor = Iconv.open("UTF-8", "ISO-8859-1")
strUTF = convertor.iconv("test")

I get:

C:\TEMP\x>ruby tryconv.rb
./iconv.rb:2: uninitialized constant Iconv (NameError)
from tryconv.rb:1:in `require'
from tryconv.rb:1

yes, i reported this recently. it's a pretty bad problem for me :O/
i hope somebody smarter than me will fix it...

emmanuel
 
P

Piergiuliano Bossi

Emmanuel said:
Piergiuliano said:
Is iconv available on Windows 1.8.0 PragProg distribution?
[...]


yes, i reported this recently. it's a pretty bad problem for me :O/
i hope somebody smarter than me will fix it...

You are right, I'm sorry, I should have checked first.

Not a big issue for me, anyway, I'm just curious to understand how it
can work on Windows.

Cheers, Giuliano
 
E

Emmanuel Touzery

Piergiuliano said:
Is iconv available on Windows 1.8.0 PragProg distribution?
[...]



yes, i reported this recently. it's a pretty bad problem for me :O/
i hope somebody smarter than me will fix it...


You are right, I'm sorry, I should have checked first.

Not a big issue for me, anyway, I'm just curious to understand how it
can work on Windows.

Cheers, Giuliano
I would like to point out that a port of this feature to windows is
possible, there is a native windows iconv port:
http://gnuwin32.sourceforge.net/packages/libiconv.htm

unfortunately I think right now ruby does not support iconv at all on win32.

emmanuel
 
N

nobu.nokada

Hi,

At Mon, 22 Dec 2003 22:21:08 +0900,
Emmanuel said:
unfortunately I think right now ruby does not support iconv at all on win32.

It's supported, if you have it installed and pass proper
options to configure.
 
E

Emmanuel Touzery

Hi,

At Mon, 22 Dec 2003 22:21:08 +0900,
Emmanuel Touzery wrote:



It's supported, if you have it installed and pass proper
options to configure.
great :O)
i hope ruby-pragprog 1.8.1 will have it by default...

emmanuel
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,364
Latest member
Stevanida

Latest Threads

Top