Character Constants

Overview

  • Character constants in MQL4 represent characters from the Unicode character set.
  • They are stored internally as ushort type, with values ranging from 0 to 65535.
  • Character constants can be defined as a single character enclosed in single quotes (e.g., '0') or as a hexadecimal ASCII code (e.g., '\x10').
  • They are numerically equivalent to their corresponding integer Unicode values.
  • Character constants can be manipulated using integer operations (addition, subtraction).
  • Definition

  • **Single Character:** Enclosed in single quotes (e.g., 'A', '0').
  • **Hexadecimal Code:** Using the \x prefix followed by 1 to 4 hexadecimal characters (e.g., '\xA9', '\x263A').
  • **Decimal Code:** Using the \d prefix followed by a decimal number from 0 to 65535.
  • Escape Sequences

    A backslash (\) is used as an escape character for special symbols:

    | Character Name | Mnemonic Code/Image | MQL4 Record | Numeric Value | |---------------------|---------------------|-------------|---------------| | New Line (Line Feed)| LF | '\n' | 10 | | Horizontal Tab | HT | '\t' | 9 | | Carriage Return | CR | '\r' | 13 | | Backslash | \ | '\\' | 92 | | Single Quote | ' | '\'' | 39 | | Double Quote | " | '\"' | 34 |

  • If a backslash is followed by an unrecognized character, the result is undefined.
  • Data Type and Range

  • **Type:** ushort (unsigned short integer).
  • **Range:** 0 to 65535.
  • Operations and Usage

  • Character constants can be cast to integers.
  • Integer operations (e.g., +, -) can be applied to character constants.
  • Characters can be added to strings using functions like StringSetCharacter().
  • Related Functions

  • StringSetCharacter()
  • StringGetCharacter()
  • ShortToString()
  • ShortArrayToString()
  • StringToShortArray()
  • Examples

  • int symbol_0 = '0'; // Assigns the numeric value 30 to symbol_0.
  • int symbol_9 = symbol_0 + 9; // Calculates the numeric value for '9'.
  • string test = "Queen\x2660Ace\x2662"; // Creates a string with Unicode characters.
  • int a = '\xA9'; // Assigns the Unicode value for '©' to variable 'a'.
  • int d = '\xAE'; // Assigns the Unicode value for '®' to variable 'd'.