Professional Documents
Culture Documents
2 9 063
ANTONIUK OLEG
Introduction
In this article we will analyze the table of symbols ASCII and the ways
it can be used. We will also deal with some new functions, the
principle of operation of which is based on the peculiarities of the
ASCII table, and then we will create a new library, which will include
these functions of MQL4 language. They are quite popular in other
programming languages, but they are not included into the list of built-
in functions. Besides, we will examine in details the basics of working
with strings. So, I think you will certainly learn something new about
this useful type of data.
What is ASCII ?
ASCII is the American coding standard for information interchange
(American Standard Code for Information Interchange). This standard is
based on the English alphabet. ASCII codes present a text in
computers, communication equipment and other devices, which work
with texts. ASCII was created in 1963, but published as a standard first
in 1967. The last amendments were made in 1986. More detailed
information about ASCII can be found here:
https://en.wikipedia.org/wiki/ASCII. Further we will see, how we can
display ASCII using MQL4 options, but first let us examine the basis of
working with strings.
//+----------------------------------------------------
//| StringExpe
https://www.mql5.com/en/articles/1474 1/11
2/12/22, 8:42 PM Strings: Table of ASCII Symbols and Its Use - MQL4 Articles
//| Copyright © 2007, Antonio Banderass. All ri
//| bande
//+----------------------------------------------------
#property copyright "Copyright © 2007, Antonio Banderas
#property link "banderassa@ukr.net"
//+----------------------------------------------------
//| start
//+----------------------------------------------------
int start()
{
MessageBox(s1);
{
}
MessageBox(s2);
return(0);
}
//+----------------------------------------------------
Determine the variable of integer type to store the string length. For
this purpose call the standard function for determination of the string
length StringLen(), which has a single parameter - the string, the
length of which we need to know.
MessageBox(s1);
Display MessageBox.
https://www.mql5.com/en/articles/1474 2/11
2/12/22, 8:42 PM Strings: Table of ASCII Symbols and Its Use - MQL4 Articles
The function returns the result in the form of a changed string. One
more important function - StringGetChar. It has two parameters:
MessageBox(s2);
Show the result. Note, that we read each symbol starting from the first
one, though it can be done vice versa. In this case we have a shorter
code and fewer variables. If in string processing it does not matter,
from which side to begin, use the following variant:
//+----------------------------------------------------
//| StringEx
//| Copyright © 2007, Antonio Banderass. All ri
//| bande
//+----------------------------------------------------
#property copyright "Copyright © 2007, Antonio Banderas
#property link "banderassa@ukr.net"
//+----------------------------------------------------
//| start
//+----------------------------------------------------
int start()
{
MessageBox(s1);
while(lenght >= 0)
{
lenght--;
}
https://www.mql5.com/en/articles/1474 3/11
2/12/22, 8:42 PM Strings: Table of ASCII Symbols and Its Use - MQL4 Articles
MessageBox(s2);
return(0);
}
//+----------------------------------------------------
You see, now instead of the cycle 'for' we use 'while', which allows
omitting the counter x. For this purpose we use the variable length.
Further we will use one of the two templates for writing functions,
depending on the fact, whether the processing sequence matters or
not. In our case we get the string with the symbols in the reverse
order, i.e. here the processing sequence matters much.
//+----------------------------------------------------
//| StringEx
//| Copyright © 2007, Antonio Banderass. All ri
//| bande
//+----------------------------------------------------
#property copyright "Copyright © 2007, Antonio Banderas
#property link "banderassa@ukr.net"
//+----------------------------------------------------
//| start
//+----------------------------------------------------
int start()
{
{
symbol = StringSetChar(symbol, 0, x);
if(x % 10 == 0)
}
MessageBox(s);
return(0);
}
//+----------------------------------------------------
The script uses in-built MQL4 functions, and string constants of a new
string and tabulation for the visualization of the table. Now compile
and start it. You will see the table of ASCII symbols:
https://www.mql5.com/en/articles/1474 4/11
2/12/22, 8:42 PM Strings: Table of ASCII Symbols and Its Use - MQL4 Articles
Look at it very attentively. You will see absolutely all symbols, which
can be needed, from numbers and letters to special symbols, some of
them can be new for you. First comes a code, and then after the sign
"=" comes the symbol itself. I marked some important sets of symbols:
https://www.mql5.com/en/articles/1474 5/11
2/12/22, 8:42 PM Strings: Table of ASCII Symbols and Its Use - MQL4 Articles
And now let us deal with new functions and in the end create a library
based on them.
string s = "AZaz";
//+----------------------------------------------------
//| StringUpperCase
//+----------------------------------------------------
string StringUpperCase(string str)
{
string s = str;
while(lenght >= 0)
{
else
}
return(s);
}
//+----------------------------------------------------
//| StringLowerCase
//+----------------------------------------------------
string StringLowerCase(string str)
{
string s = str;
while(lenght >= 0)
{
if((symbol > 64 && symbol < 91) || (symbol > 191
s = StringSetChar(s, lenght, symbol + 32);
else
https://www.mql5.com/en/articles/1474 6/11
2/12/22, 8:42 PM Strings: Table of ASCII Symbols and Its Use - MQL4 Articles
s = StringSetChar(s, lenght, symbol + 28
lenght--;
}
return(s);
}
While the sequence of processing does not matter, use the cycle while.
It is very easy to use the function, the only parameter is the string,
which should be transferred into the proper case:
string s1 = "UPPER_REGISTER_STRING";
string s2 = "lower_register_string";
MessageBox(StringLowerCase(s1)); // upper_register_st
MessageBox(StringUpperCase(s2)); // LOWER_REGISTER_ST
StringCompare
In MQL4 the comparison of strings is performed on the level of
operators using "==". Interesting is the fact that comparison is case-
dependant, i.e. strings "STRING" and "string" are different:
MessageBox("TRUE");
else
MessageBox("FALSE");
//+----------------------------------------------------
//| StringCompare
//+----------------------------------------------------
bool StringCompare(string s1, string s2)
{
return(StringLowerCase(s1) == StringLowerCase(s2));
}
Example of use:
MessageBox("TRUE");
else
MessageBox("FALSE");
StringIsDigit
This function checks the contents of the string. If the string contains
only numbers, it returns true, otherwise - false. The implementation is
quite easy and is based on the fact, that the symbols of numbers are in
one raw and have codes from 48 to 58.
https://www.mql5.com/en/articles/1474 7/11
2/12/22, 8:42 PM Strings: Table of ASCII Symbols and Its Use - MQL4 Articles
//+----------------------------------------------------
//| StringIsDigit
//+----------------------------------------------------
bool StringIsDigit(string str)
{
string s = str;
while(lenght > 0)
{
{
result = false;
break;
}
lenght--;
}
return(result);
}
Example of use:
if(StringIsDigit("1234567890")) // TRUE
MessageBox("TRUE");
else
MessageBox("FALSE");
if(StringIsDigit("1234notdigit")) // FALSE
MessageBox("TRUE");
else
MessageBox("FALSE");
StringIsAlpha
This function, like the previous one, allows determining whether the
string contains only letters. Its implementation is analogous:
//+----------------------------------------------------
//| StringIsAlpha
//+----------------------------------------------------
bool StringIsAlpha(string str)
{
string s = str;
while(lenght > 0)
{
{
result = true;
break;
}
lenght--;
}
return(result);
}
https://www.mql5.com/en/articles/1474 8/11
2/12/22, 8:42 PM Strings: Table of ASCII Symbols and Its Use - MQL4 Articles
Example of use:
if(StringIsAlpha("thereAreSomeLetters")) // TRUE
MessageBox("TRUE");
else
MessageBox("FALSE");
if(StringIsAlpha("thereAreSomeDigits12345")) // FALSE
MessageBox("TRUE");
else
MessageBox("FALSE");
Creating a Library
Now let us gather all these functions into one library. For this purpose
in MetaEditor 4 click 'File -> New -> Library -> Next'. In the field name
write stringProcess and click OK. Then insert the code of the above
described functions and save. Then create a file with the functions
prototypes, clicking 'File -> New -> Include(*. MQH) -> OK'. In the field
name write stringProcess, -> OK. Now insert the prototypes of all new
functions, and indicate the directive for import:
//+----------------------------------------------------
//| stri
//| Antonio Banderass Cop
//| bande
//+----------------------------------------------------
#property copyright "Antonio Banderass Copyright © 2007
#property link "banderassa@ukr.net"
//+---
#import "stringProcess.ex4"
//+----------------------------------------------------
//| prototypes
//+----------------------------------------------------
string StringUpperCase(string str);
//+----------------------------------------------------
//| stringProcessLi
//| Antonio Banderass Cop
//| bande
//+----------------------------------------------------
#property copyright "Antonio Banderass Copyright © 2007
#property link "banderassa@ukr.net"
#include <stringProcess.mqh>
//+----------------------------------------------------
https://www.mql5.com/en/articles/1474 9/11
2/12/22, 8:42 PM Strings: Table of ASCII Symbols and Its Use - MQL4 Articles
//| script program start function
//+----------------------------------------------------
int start()
{
if(StringIsDigit("1234567890")) // TRUE
MessageBox("TRUE");
else
MessageBox("FALSE");
if(StringIsDigit("1234notdigit")) // FALSE
MessageBox("TRUE");
else
MessageBox("FALSE");
return(0);
}
Conclusion
So, you have learned about the table of ASCII symbols and how you can
use the peculiarities of its construction for the design of new
functions. You have written new functions, which are very popular in
other programming languages, but which MQ4L does not include. Based
on them you have created a small library for string processing. I think
this library will be used not during trading, but at the receipt of
results. For example, if you are developing your own system of reports
for your Expert Advisor, then some of the functions will be useful for
you. Besides you will find many areas of use, which I may not know.
Good luck and profits to you!
Attached files |
Download ZIP
stringExperiment1.mq4
(0.96 KB)
stringExperiment2.mq4
(0.97 KB)
stringExperiment3.mq4
(0.93 KB)
stringProcess.ex4
(3.38 KB)
stringProcess.mq4
(3.18 KB)
stringProcess.mqh
(0.87 KB)
stringProcessLibraryTest.mq4
(1.01 KB)
Warning: All rights to these materials are reserved by MetaQuotes Ltd. Copying or reprinting
https://www.mql5.com/en/articles/1474 11/11