in reply to UTF-8 to ISO-8859-1
On 5.8.0 (and later), use the Encode module.$latin1 = pack 'C*', unpack 'U*', $utf8;
Some of the solutions proposed here only work well on pre 5.6 systems, because from 5.6.0 on, perl has built-in magic that automatically converts Latin1 back to UTF-8 (without you asking for it). Like this (on 5.6.1):
Result:$latin1 = 'élève'; $utf8 = chr(8801); print join ' ', $latin1, $utf8;
As you can see, the Latin1 is converted into UTF-8. This will render a lot of the code that used to work on 5.005 and earlier, useless: you can't turn UTF-8 to Latin1, as perl will undo your replacements.élève ≡
The mechanism that is behind all that, is that each string has a flag attached to it, much like the taint flag, indicating whether a string is in UTF-8 or in bytes. When you join strings of bytes to strings in UTF-8, perl will convert the bytes strings to UTF-8. The end string is marked as UTF-8 as well. Personally I really really hate this behaviour.
There are ways around it: in 5.6, using pack, you can turn a string to bytes or to UTF-8, without the bytes themselves being touched, effectively only setting or clearing this UTF-8 flag on the resulting string.
See the docs on pack for 5.6.1. Search for "C0".$bytes = pack 'C0a*', $utf8; $utf8 = pack 'U0a*', $bytes;
5.8 has less hackish methods built in. See utf8 and Encode.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re2: UTF-8 to ISO-8859-1
by dragonchild (Archbishop) on Mar 04, 2003 at 16:25 UTC | |
by bart (Canon) on Mar 05, 2003 at 01:20 UTC |