utf8 -> latin2

B

Bedo Sandor

Hi,

How can I convert utf-8 encoded strings to latin-2?
I have tried it using libuconv with little success:

require 'uconv'

class String
def un_utf8
Uconv.u8tou16(self).gsub(/\000/, '')
end

def to_utf8
tmp = ""
self.each_byte { |b|
tmp += b.chr + "\000"
}
Uconv.u16tou8(tmp)
end
end

This program is ugly, and does not exactly what I want.
u8tou16 generates a string with 16 bit long characters,
for example "test".un_utf8 == "t\000e\000s\000t\000".
gsub clears the unnecessery "\000" characters from
the string. But there are characters in Hungarian,
that has non-zero second byte in the output of the
u8tou16, so they fail to convert. Anyway this is an
ugly hack.

How is it done nicely?
 
W

Wesley J Landaker

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

How can I convert utf-8 encoded strings to latin-2?
I have tried it using libuconv with little success:

require 'uconv'

class String
def un_utf8
Uconv.u8tou16(self).gsub(/\000/, '')
end

def to_utf8
tmp = ""
self.each_byte { |b|
tmp += b.chr + "\000"
}
Uconv.u16tou8(tmp)
end
end

This program is ugly, and does not exactly what I want.
u8tou16 generates a string with 16 bit long characters,
for example "test".un_utf8 == "t\000e\000s\000t\000".
gsub clears the unnecessery "\000" characters from
the string. But there are characters in Hungarian,
that has non-zero second byte in the output of the
u8tou16, so they fail to convert. Anyway this is an
ugly hack.

How is it done nicely?

I think the iconv module handles this nicely:

require 'iconv'
Iconv.conv("utf-8","latin2","this is a test")

- --
Wesley J. Landaker - (e-mail address removed)
OpenPGP FP: 4135 2A3B 4726 ACC5 9094 0097 F0A9 8A4C 4CD6 E3D2

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/tPU68KmKTEzW49IRAj8nAJ9QgkOiYp4UKf7gFBFrUW6qm0NW8gCfXMIy
fz4uq1FmNXxDvhkGSdyWJIA=
=ntbU
-----END PGP SIGNATURE-----
 

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,139
Messages
2,570,807
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top