Skip to content

Instantly share code, notes, and snippets.

@felsenhower
Last active March 3, 2021 11:02
Show Gist options
  • Save felsenhower/e103a0f6c2354b9d74afd9e6e60c16c5 to your computer and use it in GitHub Desktop.
Save felsenhower/e103a0f6c2354b9d74afd9e6e60c16c5 to your computer and use it in GitHub Desktop.
Script to convert letters to their full-width counterparts
#!/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