Name

Encode::Unicode::PerlDecodeJava - Encode a Unicode string in Perl and decode it in Java

Synopsis 𝝰

use Encode::Unicode::PerlDecodeJava;

ok $_ eq decode93(encode93($_)) for(qw(aaa (𝝰𝝱𝝲) aaa𝝰𝝱𝝲aaa yüz))

Description

encode93($input)

encodes any Perl string given as $input, even one containing Unicode characters, using only the 93 well known ASCII characters below:

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789 '()[]{}<>`!@#$%^&*_-+=,;:|.?\

and returns the resulting encoded string.

Such a string can be easily compressed and transported using software restricted to ASCII data and then reconstituted as a Unicode string in Perl by using decode93() or in Java by using the code reproduced further below.

decode93($input)

takes an $input string encoded by encode93() and returns the decoded string.

The following Java code takes a string encoded by encode93() and (inefficiently) returns the decoded string to Java:

String decode93(String input)                                                 // Decode string encoded by encode93()
 {final StringBuilder s = new StringBuilder();
  final StringBuilder n = new StringBuilder();
  final int           N = input.length();

  for(int i = 0; i < N; ++i)                                                  // Decode each character
   {char c = input.charAt(i);
    if (Character.isDigit(c)) n.append(c);                                    // Digit to accumulate
    else if (c == '~')                                                        // Decode number
     {final int p = Integer.parseInt(n.toString());
      s.appendCodePoint(p);
      n.setLength(0);
     }
    else                                                                      // Letter
     {if (n.length() > 0)                                                     // Number available for decode
       {final int p = Integer.parseInt(n.toString());
        s.appendCodePoint(p);
        n.setLength(0);
       }
      s.append(c);                                                            // Add letter
     }
   }
  if (n.length() > 0)                                                         // Trailing number available for decode
   {final int p = Integer.parseInt(n.toString());
    s.appendCodePoint(p);
    n.setLength(0);
   }
  return s.toString();                                                        // Decoded string
 }

Installation

Standard Module::Build process for building and installing modules:

perl Build.PL
./Build
./Build test
./Build install

Author

philiprbrenan@gmail.com

http://www.appaapps.com

Copyright

Copyright (c) 2017 Philip R Brenan.

This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.