Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert special characters to UTF-8?

Tags:

perl

When I extract data from a MySQL database, some of the output have special characters, when opened in e.g. emacs it decodes to \240 and \346.

When shown in an UTF-8 terminal, the special characters is shown as

So the used encoding seams to only use 1 byte per character.

I can e.g. see that \346 should be æ.

Question

Does Perl have a module that can encode these special characters to UTF-8?

like image 542
Sandra Schlichting Avatar asked Oct 24 '22 06:10

Sandra Schlichting


1 Answers

Use Encode::decode to decode your data from whatever encoding it's in to Perl's internal format.

Then, when writing the data out to a file, set the 'utf8' layer to make the data be written in UTF-8.

use Encode;

my $data_from_database = ...;

my $perl_data = decode('ISO-8859-1', $data_from_database);

binmode STDOUT, ':utf8';

print $perl_data;
like image 84
zgpmax Avatar answered Oct 26 '22 22:10

zgpmax