Character code

4D - Documentation   Français   English   German   Spanish   4D v11 SQL, Command Theme List   4D v11 SQL, Command Alphabetical List   4D v11 SQL, Constant Theme List   Back   Previous   Next

version 11 (Modified)


Character code (character) Number

ParameterTypeDescription
characterStringCharacter for which you want to get the code
Function resultNumberCharacter code

Description

The Character code command returns the current character code of character.

If the database is operating in Unicode mode (default mode for databases created with version 11 and higher of 4D), the command returns the Unicode UTF-16 code of character (included between 1 and 65535).

If the database is operating in ASCII compatibility mode, the command returns the ASCII code of character (included between 0 and 255).

For more information about the different modes for managing strings in 4D, please refer to the About Unicode section.

If there is more than one character in the string, Character code returns only the code of the first character.

The Char function is the counterpart of Character code. It returns the character that a UTF-16 or ASCII code represents.

Important: In ASCII compatibility mode, all the text values, fields, or variables that you have not yet converted to another ASCII map are Mac OS-encoded on both Macintosh and Windows. For more information, see the section ASCII Codes.

Examples

1. Uppercase and lowercase characters are considered equal within a comparison. You can use Character code to differentiate between uppercase and lowercase characters. Thus, this line returns True:

   ("A" = "a")

On the other hand, this line returns False:

   (Character code("A")=Character code("a"))

2. This example returns the code of the first character of the string "ABC":

   GetCode:=Character code("ABC")  ` GetCode gets 65, the character code of A

3. The following example tests for carriage returns and tabs:

   For($vlChar;1;Length(vtText))
      Case of
         : (vtText[[$vlChar]]=Char(Carriage return))
            ` Do something
         : (vtText[[$vlChar]]=Char(Tab))
            ` Do something else
         : (...)
            ` ...
      End case
   End for

When executed multiple times on large texts, this test will run faster when compiled if it is written this way:

   For($vlChar;1;Length(vtText))
      $vlCode:=Character code(vtText[[$vlChar]])
      Case of
         : ($vlCode=Carriage return)
            ` Do something
         : ($vlCode=Tab)
            ` Do something else
         : (...)
            ` ...
      End case
   End for

The second piece of code runs faster for two reasons: it does only one character reference by iteration and uses LongInt comparisons instead of string comparisons to test for carriage returns and tabs. Use this technique when working with common codes such as CR and TAB.

See Also

ASCII Codes, Char, Character Reference Symbols.


4D - Documentation   Français   English   German   Spanish   4D v11 SQL, Command Theme List   4D v11 SQL, Command Alphabetical List   4D v11 SQL, Constant Theme List   Back   Previous   Next