字符常量

字符作为MQL5中的字符串要素是统一字符设置的指数。它们是可以转换成整数的十六进制值,可以像加减法定理一样进行整数操作

引号里的任何单一特质和十六进制的 ASCII 代码'\x10' 都能当做字符常量无符号短整型 型,例如,0型记录的数值是30,相当于在图标字符中代表调整归零。

示例:

void OnStart()

  {

//--- 定义字符常量

   int symbol_0='0';

   int symbol_9=symbol_0+9; // 获得符号 '9'

//--- 输出常量值 

   printf("In a decimal form: symbol_0 = %d,  symbol_9 = %d",symbol_0,symbol_9);

   printf("In a hexadecimal form: symbol_0 = 0x%x,  symbol_9 = 0x%x",symbol_0,symbol_9);

//--- 输入常量成字符串

   string test="";

   StringSetCharacter(test,0,symbol_0);

   StringSetCharacter(test,1,symbol_9);

//--- 这是字符串中看起来像的东西

   Print(test);

  }

在程序源文本与常量型进行处理时,反斜线符号是编译器的控制字符,,一些符号,例如,单引号('),双引号("),反斜杠(\)和控制字符都能被当做以反斜杠(\)为起点的符号的集合,如下表:

字符名称 助记码或者图像 MQL5中的记录 数值
新线 (换行) LF '\n' 10
水平制表符 HT '\t' 9
回车 CR '\r' 13
反斜杠 \ '\\' 92
单引号 ' '\'' 39
双引号 " '\"' 34
十六进制代码 hhhh '\xhhhh' 1 to 4 十六进制字符
十进制代码 d '\d' 从 0 到 65535的十进制代码

如果反斜杠后面跟着另一种字符而不是以上描述的类型,结果是未知的。

示例

void OnStart()

  {

//--- 声明字符常量

   int a='A';

   int b='$';

   int c='©';      // 代码 0xA9

   int d='\xAE';   // 符号代码 ®

//--- 输出打印常量

   Print(a,b,c,d);

//--- 添加字符到字符串

   string test="";

   StringSetCharacter(test,0,a);

   Print(test);

//--- 成串的替换字符

   StringSetCharacter(test,0,b);

   Print(test);

//--- 成串的替换字符

   StringSetCharacter(test,0,c);

   Print(test);

//--- 成串的替换字符

   StringSetCharacter(test,0,d);

   Print(test);

//--- 字符表示为数字

   int a1=65;

   int b1=36;

   int c1=169;

   int d1=174;

//--- 添加字符到字符串

   StringSetCharacter(test,1,a1);

   Print(test);

//--- 添加字符到字符串

   StringSetCharacter(test,1,b1);

   Print(test);

//--- 添加字符到字符串

   StringSetCharacter(test,1,c1);

   Print(test);

//--- 添加字符到字符串

   StringSetCharacter(test,1,d1);

   Print(test);

  }

综上所述,常量(或变量)字符的值是字符表中的指标,指标存在整型,能以不同的方式输出。

void OnStart()

  {

//--- 

   int a=0xAE;     // 代码 ® 符合于 the '\xAE' 文字

   int b=0x24;     // 代码 $ 符合于 the '\x24' 文字

   int c=0xA9;     // 代码 © 符合于 to the '\xA9' 文字

   int d=0x263A;   // 代码 ☺ 符合于 the '\x263A' 文字

//--- 显示值

   Print(a,b,c,d);

//--- 添加字符到字符串

   string test="";

   StringSetCharacter(test,0,a);

   Print(test);

//--- 成串的替换字符

   StringSetCharacter(test,0,b);

   Print(test);

//--- 成串的替换字符

   StringSetCharacter(test,0,c);

   Print(test);

//--- 成串的替换字符

   StringSetCharacter(test,0,d);

   Print(test);

//--- 合适的代码

   int a1=0x2660;

   int b1=0x2661;

   int c1=0x2662;

   int d1=0x2663;

//--- 添加黑桃字符

   StringSetCharacter(test,1,a1);

   Print(test);

//--- 添加红心字符

   StringSetCharacter(test,2,b1);

   Print(test);

//--- 添加方片字符

   StringSetCharacter(test,3,c1);

   Print(test);

//--- 添加梅花字符

   StringSetCharacter(test,4,d1);

   Print(test);

//--- 成串的字符文字示例

   test="Queen\x2660Ace\x2662";

   printf("%s",test);

  }

字符型的内部表示法是 无符号短整型 ,字符常量能够接受从0到65535的值。

另见

StringSetCharacter(), StringGetCharacter(), ShortToString(), ShortArrayToString(), StringToShortArray()