Errors, bugs, questions - page 3011

 
A100:

Because the 4th parameter

I need to copy the first 3 characters. If I wanted to copy the whole string (7 characters including the ending 0), I would specify -1. And if some of it cannot be copied, what for does this parameter do I need it for?

This parameter is not to specify how many characters you want to copy, like the
sub-string, but to specify how many characters are passed to the array.
i.e. it's a C-like strict security check on the size of the data being passed.
This size will automatically allocate memory for the dynamic array.

If you want to copy three elements from a string, you have to get them from the string first,
, and then pass them in, specifying the size as much as you pass.

 
Roman:

This parameter is not to specify how many characters you want to copy, like the
sub-string, but to specify how many characters are passed to the array.
i.e. it is a C-size strict security check on the size of the data being passed.
This size will automatically allocate memory for the dynamic array.

If you need to copy three elements from a string, you have to get them from the string first,
, and then you have to pass them in, specifying the size as much as you pass.

You mean like this?

StringToCharArray(InputText,scr,0,StringLen(InputText));
 
Vitaly Muzichenko:

I mean, right?

Plus one as the size of the terminating zero.
StringLen returns the size without including the zero.

char ch[];
string InputText = "ABCDEF";
int    size      =  StringLen(InputText)+1;
    
StringToCharArray( InputText, ch, 0, size);
Print(GetLastError()); //0
 
Roman:

Plus one as the size of the terminating zero.
StringLen returns the size without regard to zero.

I gave an example from the developers and it works

Forum on trading, automated trading systems & strategy testing

Libraries: Library of hash functions and conversions

Ilyas, 2021.03.04 15:12

StringToArray function captures termination zero if string length is not specified (historically)

Therefore, in the CryptDecodeA function you should use

StringToCharArray(InputText,scr,0,StringLen(InputText));

instead of

StringToCharArray(InputText,scr);

 
Vitaly Muzichenko:

I gave an example from the developers and it works.


The use of libraries was not intended in this question ))
This is the nuances of libraries.

 
Roman:

Plus one as the size of the terminating zero.
StringLen returns the size without regard to zero.

Is StringToCharArray so incapable of retrieving the string length from a string (especially since you don't need to calculate it in MQL) that external help in the form of an additional parameter is required? And why other MQL functions do not have such supposedly strict"C-check"?

 
A100:

Is StringToCharArray so incapable of extracting string length from string type (especially since it doesn't need to be calculated in MQL) that external help in the form of an additional parameter is required? And why other MQL functions do not have such an allegedly strict check?

This is probably not a question for me, but for the developers.
But this parameter is used to allocate memory size for a dynamic array.
In other words, it's a convenient way to avoid allocating memory for the array by yourself.
Plus, we can check for correctness.
The additional size parameter is a standard practice of rigorous security in sys.
In other functions where size is not required, this parameter is absent.
Usually this parameter is used when working with arrays to allocate memory.
This is what happens in this case.

 
Roman:

This is probably not a question for me, but for the developers.
But this parameter is used to allocate memory for a dynamic array.
It means that it is convenient for you not to allocate memory of the array by yourself.
Plus we will get a check for correctness.

And what is check for correctness in? - Suppose I, for example, wrote 0 - a deliberately incorrect value.

void OnStart()
{
    uchar ch[];
    const string text = "ABCDEF";
    Print( StringToCharArray( text, ch, 0, 0 ));
}

And everything is copied correctly and the result: 7 - no error

 
A100:

What is the check for correctness? - So, let's say that I set it to 0, which is a known incorrect value.

And everything is copied correctly. Result: 7 - no error

The trick is in the function description ))

count=-1

[in]  Количество элементов массива для копирования. Определяет длину результатной строки. 
По умолчанию -1, что означает копирование до конца массива, либо до встречи терминального 0. 
Терминальный 0 также будет скопирован в массив-приемник, при этом размер динамического массива может быть увеличен при необходимости под размер строки. 
Если размер динамического массива больше длины строки, то размер массива уменьшен не будет. 

automatic progress ))
which confuses the user more.

 
Roman:

This parameter is not to specify how many characters you want to copy as a sub-string
but to specify how many characters are passed to the array.

help disagrees with this interpretation.

The number of array elements to copy. Defines the length of the result string. The default is -1, which means it will copy to the end of the array, or the terminal 0. The terminal 0 will also be copied into the destination array, and the size of the dynamic array can be increased to match the string size if needed. If the size of the dynamic array is greater than the length of the string, then the size of the array will not be reduced.

The description, of course, is atas - string is referred to as an array, array as a string, confusion

Number of array (string) elements to copy. Specifies the length of the resulting string (array). Defaults to -1, which means copying to the end of the array (string), or meeting the terminal 0.

Reason: