Skip to content

$Char()

$Char()

Returns a string of one or more characters corresponding to integer ASCII codes specified in its argument(s). For a process started in UTF-8 mode, $CHAR() returns a string composed of characters represented by the integer equivalents of the Unicode code-points specified in its argument(s).

The format for the $CHAR function is:

$C[HAR](intexpr[,...])
  • The integer expression(s) specify the codes of the character(s) $CHAR() returns.
  • The M standard does not restrict the number of arguments to $CHAR(). However, GT.M does limit the number of arguments to a maximum of 254. $CHAR() provides a means of producing non-graphic characters, as such characters cannot appear directly within an M string literal. When used with $ASCII(), $CHAR() can also perform arithmetic operations on the codes associated with characters.
  • With VIEW “BADCHAR” enabled, $CHAR() produces a run-time error if any expression evaluates to a code-point value that is not a UTF-8 character. GT.M determines from ICU which characters are illegal.
  • $ZCHAR() is the parallel function of $CHAR(). $ZCHAR() returns a sequence of bytes (rather than a sequence of characters) and can perform all byte-oriented $CHAR() operations. For more information, refer to “$ZCHar()”.

Examples of $CHAR()

Example:

GTM>write $char(77,85,77,80,83,7)
MUMPS
GTM>

This example uses $CHAR() to WRITE the word MUMPS and signal the terminal bell.

Example:

set nam=$extract(nam,1,$length(nam)-1)_$char($ascii(nam,$length(nam))-1)

This example uses $CHAR() and $ASCII() to set the variable nam to a value that immediately precedes its previous value in the set of strings of the same length as nam.

Example:

GTM>write $zchset
UTF-8
GTM>write $char(20027)
主
GTM>write $char(65)
A

In the above example, the integer value 20027 is the Unicode character “主” in the CJK Ideograph block. Note that the output of the $CHAR() function for values of integer expression(s) from 0 through 127 does not vary with choice of the character encoding scheme. This is because 7-bit ASCII is a proper subset of UTF-8 character encoding scheme. The representation of characters returned by the $CHAR() function for values 128 through 255 differ for each character encoding scheme.