U (String function)

From m204wiki
(Redirected from U method)
Jump to navigation Jump to search

Convert EBCDIC string to Unicode constant, including character encoding (String class)


The U intrinsic method converts an EBCDIC string, which may include XML character and entity references, to a Unicode string. The function also converts XML style hexadecimal character references and XHTML entity references to the represented Unicode character. Since in use the method acts like a Unicode constant, it is also included in the list of Constant methods.

Syntax

%unicode = string:U

Syntax terms

%unicode A Unicode variable to receive the Unicode string represented by the method object string.
string A constant character string value, which may include an XML-style hexadecimal character reference or an XHTML entity reference. That is, string may contain an ampersand (&) in the following cases:
  • At the start of the substring &. This substring is converted to a single ampersand (&) character.
  • At the start of a hexadecimal character reference: for example, the eight characters “ for the Unicode "Left double quotation mark" (). The character reference is converted to the referenced character.
  • As of Sirius Mods Version 7.6, an XHTML entity reference (for example, the six characters   for the "non-breaking-space" character). The entity reference is converted to the referenced character.
A decimal character reference (for example, ¬) is not allowed.

Usage notes

  • U is a compile-time-only equivalent of the EbcdicToUnicode method of the intrinsic string class (with its CharacterDecode argument implicitly set to True).
  • Using the U method (or EbcdicToUnicode) is necessary for converting to type Unicode if the string you want to convert may contain a hexadecimal character reference. Such a reference cannot be meaningfully assigned to a Unicode variable otherwise, whereas keyboard-available characters can simply be assigned directly to a Unicode variable without character reference and without conversion by U.
  • The U method is available as of Sirius Mods Version 7.3.

Examples

  1. The following Print statement displays a plus sign (+):

    %p Unicode Initial('+') print %p

  2. The following Print statement displays a copyright sign (©):

    %copy Unicode Initial('©':U) print %copy

  3. The following Print statement displays 2122:

    %tm Unicode Initial('™':U) print %tm:UnicodeToUtf16:StringToHex

    Note: Simply specifying print %tm in this example would produce an attempt to convert the Unicode character to EBCDIC for printing, but since the Unicode trademark character does not translate to a valid EBCDIC character, the Print output would be the character reference for the trademark: ™.

  4. Say you want to use the following XPath expression within a Janus SOAP method:

    */pers[@name="Rebecca"]

    This selects pers children elements with the name attribute equal to Rebecca. However, simply typing the square brackets ([ ]) on your terminal keyboard is vulnerable to an XPath "compilation" error when the method using that expression is executed. The error occurs if you have a mismatch between the 3270 configuration on your PC and the codepage setting (as can be determined by the UNICODE command) in your Online.

    To avoid this potential error, you can use the U method with the [ and ] XHTML entities added in Model 204 7.6:

    %doc:print('*/pers[@name="Rebecca"]':u)

    Using the XHTML entities like this specifies the square brackets in a way that is sure to be correct. The U method treats the entities as their character equivalents and creates a Unicode constant string as the argument of the method.

    An alternative bracket substitution is to use Static %variables initialized to the correct values, for example:

    %lsq is string len 1 static initial('['):u %rsq is string len 1 static initial(']'):u

    But this approach requires declaring a pair of variables (within the scope of the method/subroutine), and it performs a run-time execution of two concatenations as well as conversion from EBCDIC to Unicode.

    For more information about square bracket translations, see this short summary.

See also