Last active
March 3, 2021 11:02
-
-
Save felsenhower/e103a0f6c2354b9d74afd9e6e60c16c5 to your computer and use it in GitHub Desktop.
Script to convert letters to their full-width counterparts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/perl | |
# This perl script converts ASCII characters between 0x21 ("!") and 0x7E ("~") to full-width characters. | |
# https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms | |
# All other characters will be ignored! | |
# Examples: | |
# $ echo 'Lorem ipsum dolor sit amet, consectetur, adipisci velit' | ./full-width | |
# Lorem ipsum dolor sit amet, consectetur, adipisci velit | |
# $ echo 'Hello, Mr. Schrödinger! What does "ħ" mean?' | ./full-width | |
# Hello, Mr. Schrödinger! What does "ħ" mean? | |
use strict; | |
use feature 'unicode_strings'; | |
binmode STDOUT, ":utf8"; | |
binmode STDIN, ":utf8"; | |
while (my $line = <>) { | |
chomp($line); | |
for my $char (split //, $line) { | |
my $ordinal = ord("$char"); | |
if ($ordinal >= 0x21 && $ordinal <= 0x7E) { | |
print chr("$ordinal" + 0xFF01 - 0x21); | |
} else { | |
print "$char"; | |
} | |
} | |
print "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment