in reply to Re: ASCII (latin1) to UTF-8 (with sub latin1_to_utf8 & utf8_to_latin1)
in thread ASCII to UTF-8

I was needing something cheap that would work on both 5.6.x and 5.8.x versions. (I'm trying to keep the code the same while moving back and forth between systems) Tried your utf8_to_latin1() and ran into problems on 5.8.x. The below seems to work on both versions. Thanks for the code.

sub utf8_to_latin1 { # return( pack("U0C*", unpack( "U*",@_[0]) ) ); return( pack( "C*", unpack("U0U*",@_[0]) ) ); }


Updated: (see sig) So I go and feed the original (commented out) line into the big program and it fails. It worked in the test program on both 5.6 and 5.8. Wander all over and find the 5.8 perluniintro where they explicitly say

$native_string = pack("C*", unpack("U*", $Unicode_string));
just like the original poster. But that didn't work alike on both Perl versions. Played around some more and hit upon the other variant above. This now works in both test and 'real' programs on both Perl versions. (sigh)

--
I'm a pessimist about probabilities; I'm an optimist about possibilities. - - Lewis Mumford