Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Perl to convert normal letter to wide character

How for perl to convert from halfwidth to fullwidth form? In perl‘s term, from normal character to wide character.

E.g., from !abc ABC! to !abc ABC!.
Arithmetically, the following should be correct

binmode STDOUT, ":utf8";

$letter = 'A';
$lw = chr($letter + 0xfee0);
print "Hello $lw ($letter)!\n";

But the print out is not what I was hoping for, which should be Hello A (A)!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you had used use warnings you would have been given the warning that your addition failed.

Argument "A" isn't numeric in addition (+) at ...

Which means that your letter ‘A’ is cast into a number before addition, which will be 0 in this case. You can verify that by removing $letter + and just run the number in chr.

You might fix your issue by fixing the logic:

my $lw = chr(ord ($letter) + 0xfee0);

First changing the letter into numeric, and then doing addition.

Note that to avoid simple mistakes like this, you should always use these pragmas:

use strict;
use warnings;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading