You can use the ascii() operator of the Encoding static class when you need the numeric ASCII value of a character while writing OCL expressions.
What ascii() returns
The ascii() operator converts a character to its ASCII (American Standard Code for Information Interchange) numeric value. Use it with a single-character string when you need to compare that character by its numeric code.
For example, the ASCII value for A is 65. The values for uppercase English letters run from 65 (A) through 90 (Z).
| Character | ASCII value |
|---|---|
A
|
65
|
Z
|
90
|
a
|
97
|
z
|
122
|
0
|
48
|
9
|
57
|
When to use it
Use ascii() when a rule depends on an ASCII range rather than on a literal character comparison. Typical uses include:
- Checking whether a character is an uppercase English letter.
- Checking whether a character is a digit.
- Distinguishing uppercase from lowercase characters.
- Applying a numeric ordering rule to ASCII characters.
Example: validate the first character of a department code
Assume that Department has a string attribute named Code. First obtain the first character of Code, then apply ascii() to that character.
The validation is true when the resulting value is between 65 and 90, inclusive. For example:
- A code beginning with
A, such asAccounts, passes becauseAis65. - A code beginning with
Z, such asZonal, passes becauseZis90. - A code beginning with
a, such asaccounts, does not pass becauseais97. - A code beginning with
1, such as123, does not pass because1is49.
Before evaluating a first character, make sure that the code contains at least one character. Use size() to test the string length.
Limits and considerations
ASCII covers the basic Latin alphabet, digits, and selected punctuation and control characters. Do not use ASCII ranges as a general test for letters in names or codes that can contain non-ASCII characters such as é, ñ, or emoji.
Use the OCL editor to inspect the operators available for the applicable type and to confirm the expression syntax in your MDriven version. For general operator discovery, see Documentation:OCL General Operators.
Related encoding operations
ascii() returns a numeric character value. It does not convert an entire string to a byte array. For byte conversion, see GetBytes. For UTF-7 encoding, see UTF7.
