http://www.perlmonks.org?node_id=1161729


in reply to How to convert a string like this: "\321\201\321\202\321\200\321\203\320\272\321\202\321\203\321\200\320\260" to a proper utf8?

Take a look at Encode::DoubleEncodedUTF8. The following returns your original string:

#!/usr/bin/env perl use 5.18.0; use warnings; use utf8::all; use Encode; use Encode::DoubleEncodedUTF8; my $bytes = "\321\201\321\202\321\200\321\203\320\272\321\202\321\203\ +321\200\320\260"; my $fixed = decode( "utf-8-de", $bytes ); say $fixed;

Update: A strong word of caution about the above. Fixing "broken" Unicode data is fraught with error, particularly if you can't guarantee the source of the error. If this bad data is coming from outside your program, then try to contact whoever generates that bad data and see if you can get it fixed there. Otherwise, you may have to trust yourself to be lucky on this.

However, if the bad data is being generated from within your program, try to fix the underlying bug before you try to "fix" the bad strings in question. It will save you much grief in the long run.