How to write a utf8 string as binary

115 Views Asked by At

I have an epub that has been double encoded to utf8. I wanted to fix it by decoding it once and write it as binary but I get "wide character in subroutine" error. I'm using Mojo::File.

use Mojo::Base -strict;
use Mojo::File;
use Encode qw( decode_utf8 encode_utf8 );

my $in = Mojo::File->new('part0000.html')->slurp();
my $out = decode_utf8($in);
utf8::downgrade($out);
Mojo::File->new('string.html')->spurt($out);

I get the same error if I try this.

my $in = Mojo::File->new('part0000.html')->slurp();
my $out = encode_utf8(decode_utf8(decode_utf8($in)));
Mojo::File->new('string.htm')->spurt($out);

The decode seems to work but I can't figure out how to write it out as binary.
Any suggestions appreciated. part0000.html

1

There are 1 best solutions below

1
shirha On

Apparently, it's impossible to simply write a utf8 string as binary in Perl because of "wide character". I solved it a different way with regex.