Java & MQL4

 
Dear MetaTrader Developers,

I have to write a binary file in JAVA language and read it using MQL, but i am facing an incompatibility problem.

I am writing a simple integer ("400") value into a binary file ("data. dat") using the following Java code:

File f = new File("data.dat");
 
FileOutputStream fos = new FileOutputStream(f);        
DataOutputStream out = new DataOutputStream(fos); 
        
out.writeInt(400); // Java integer (4 bytes)
 
out.close();
fos.close();
So i am trying to read this file using the following MQL code:

ExtHandle = FileOpen("data.dat", FILE_BIN|FILE_READ); // File placed on experts/files directory
 
if(ExtHandle < 0) return(-1);
 
int number = FileReadInteger(ExtHandle, LONG_VALUE); // MQL integer (4 byes)
 
Comment(number); // The output is 83946924 ... not 400. Why ?????
But the output is a number 83946924 !!!

How can i solve this problem??

Thanks & Regards,
 

Does Java write the integer in a compatible byte order?


Have MT4 write the same file. Use a binary file editor to compare the java file and the MT4 file for byte ordering.


int start(){
 
   int file = FileOpen("Test.bin", FILE_WRITE|FILE_BIN);      
   FileWriteInteger(file, 400, LONG_VALUE);      
   FileClose(file);
      
   file = FileOpen("Test.bin", FILE_READ|FILE_BIN);      
   int value = FileReadInteger(file, LONG_VALUE);     
   Print(" Value = ", value);  
    
   // Output to terminal   2008.01.19 14:47:47    WriteTest EURJPY,M5:  Value = 400   
 
   return(0);
}        
 
 

Dump of the binary file written by MT4...

File Address 00000000

4 hex bytes of data 90 01 00 00

$ dump "C:\Program Files (x86)\Metatrader\Metatrader\experts\files\Test.bin"
C:\Program Files (x86)\Metatrader\Metatrader\experts\files\Test.bin:
00000000  9001 0000  ....

Java created file must be same bytes for it to be compatible

 
phy,

thank you very much!

Java writes the integer on inverse order. I have changed the Java code and it is working now:

out.writeInt(Integer.reverseBytes(400));
Thanks again and best regards!
 

Your flag is Brazil... I'm listening to Hermeto right now...

Reason: