MQL5 - Problem about StringToCharArray and CharArrayToString Functions

 
int OnInit()
  {
           
      string result="20140201";  // The seperated strings must be --->  2014 02 01   but the result is  ---> 2014 20 20
      Print("The date = ",result);
      
      // ----- Seperate Year -------------------
      uchar date_yearx[]={};  
      StringToCharArray(result,date_yearx,0,4,65001);
      string date_year=CharArrayToString(date_yearx,0,4,65001);
      Print("Date_year = ",date_year);

      // ----- Seperate month -------------------
      uchar date_monthx[]={};  
      StringToCharArray(result,date_monthx,4,2,65001);
      string date_month=CharArrayToString(date_monthx,4,2,65001);
      Print("Date_month= ",date_month);

      // ----- Seperate day -------------------
      uchar date_dayx[]={};  
      StringToCharArray(result,date_dayx,6,2,65001);
      string date_day=CharArrayToString(date_dayx,6,2,65001);
      Print("Date_day = ",date_day);

   

   return(INIT_SUCCEEDED);
  }

Hello, I would like to seperate year, month and day from date but the code does not work properly. Could you please help me.

 

Why do you want to use StringToCharArray() and CharArrayToString() at all ?

You can either convert your string to a datetime with StringToTime() or extract your data directly from the string using StringSubstr().

 

I'm willing to convert a historical data txt file to MT5. The text file structure is as follows:

201402101130,780,00,780,00,779,75,779,75,77,6004,1











I'm reading from file and willing to convert to below structure and write to a new file as date, time, open,close,high,low,volume

   2014.02.01 , 11:30 , 780.0 , 780,0, 779.75, 779.75 , 6004

So I need more string operations.


I think function always starts with start=0 altough I'm willng to use a different position.

int  StringToCharArray( 
   string  text_string,         // source string 
   uchar&  array[],             // array 
   int     start=0,             // starting position in the array 
   int     count=-1             // number of symbols 
   uint    codepage=CP_ACP      // code page 
   );

Could you please suggest me a function/method so that I could split and than merge the strings according to my needs.

Thank you.

 
AED71 #:

I'm willing to convert a historical data txt file to MT5. The text file structure is as follows:

201402101130,780,00,780,00,779,75,779,75,77,6004,1











I'm reading from file and willing to convert to below structure and write to a new file as date, time, open,close,high,low,volume

   2014.02.01 , 11:30 , 780.0 , 780,0, 779.75, 779.75 , 6004

So I need more string operations.


I think function always starts with start=0 altough I'm willng to use a different position.

Could you please suggest me a function/method so that I could split and than merge the strings according to my needs.

Thank you.

I already answered you. You don't need to user char array at all.

Use StringSplit(), StringSubstr(). StringToTime(), StringToDouble()...

 

AED71 #: I'm willing to convert a historical data txt file to MT5. The text file structure is as follows: 201402101130,780,00,780,00,779,75,779,75,77,6004,

Besides Alain's answer in the previous post, I would also like to add that you can read the file as a CSV and have the fields automatically split up as well.
 
Hi Alain and Fernando, thank you for your help and comments. I solved the issue. Regards.
 
AED71 #: I solved the issue.

Don't do that. Someone searching might find this thread and still be clueless. What was the problem? What solved what?

How To Ask Questions The Smart Way. (2004)
     When You Ask.
          Follow up with a brief note on the solution.

Reason: