Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Transform ASCII into UniCode

by kcott (Archbishop)
on Mar 24, 2021 at 19:48 UTC ( [id://11130290]=note: print w/replies, xml ) Need Help??


in reply to Transform ASCII into UniCode

G'day Perlian,

Here's a generic technique for dealing with this type of problem which doesn't require listing every character.

$ perl -Mutf8 -C -E '
    my ($offset_0, $offset_A, $offset_a)
        = (ord("𝟎")-ord("0"), ord("𝐀")-ord("A"), ord("𝐚")-ord("a"));
    say "The quick brown fox jumps over the lazy dog 1234567890 times."
        =~ s/([0-9])/chr(ord($1)+$offset_0)/egr
        =~ s/([A-Z])/chr(ord($1)+$offset_A)/egr
        =~ s/([a-z])/chr(ord($1)+$offset_a)/egr;
'
𝐓𝐡𝐞 𝐪𝐮𝐢𝐜𝐤 𝐛𝐫𝐨𝐰𝐧 𝐟𝐨𝐱 𝐣𝐮𝐦𝐩𝐬 𝐨𝐯𝐞𝐫 𝐭𝐡𝐞 𝐥𝐚𝐳𝐲 𝐝𝐨𝐠 𝟏𝟐𝟑𝟒𝟓𝟔𝟕𝟖𝟗𝟎 𝐭𝐢𝐦𝐞𝐬.

This should work fine with your 5.26.3 (I'm using 5.32.0). As general information: say requires 5.10 and /r requires 5.14.

Two caveats:

  • Different Perl versions support different Unicode® versions: check you have a sufficiently high version of Perl to handle the Unicode characters you want to output (if in doubt, check the deltas).
  • Some alphabetical sequences in [PDF] "Mathematical Alphanumeric Symbols" have missing characters because they were defined in earlier versions. The first example in that block is U+1D44E (𝑎) to U+1D467 (𝑧) which has U+1D455 (<reserved>) because U+210E () was already defined in [PDF] "Letterlike Symbols" as PLANCK CONSTANT.

Here's another example to show the generality of the solution. Only three characters were changed in the code to produce completely different output.

$ perl -Mutf8 -C -E '
    my ($offset_0, $offset_A, $offset_a)
        = (ord("𝟘")-ord("0"), ord("𝕬")-ord("A"), ord("𝖆")-ord("a"));
    say "The quick brown fox jumps over the lazy dog 1234567890 times."
        =~ s/([0-9])/chr(ord($1)+$offset_0)/egr
        =~ s/([A-Z])/chr(ord($1)+$offset_A)/egr
        =~ s/([a-z])/chr(ord($1)+$offset_a)/egr;
'
𝕿𝖍𝖊 𝖖𝖚𝖎𝖈𝖐 𝖇𝖗𝖔𝖜𝖓 𝖋𝖔𝖝 𝖏𝖚𝖒𝖕𝖘 𝖔𝖛𝖊𝖗 𝖙𝖍𝖊 𝖑𝖆𝖟𝖞 𝖉𝖔𝖌 𝟙𝟚𝟛𝟜𝟝𝟞𝟟𝟠𝟡𝟘 𝖙𝖎𝖒𝖊𝖘.

— Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130290]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2024-04-23 14:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found