MQL5 Struct as uchar[]

 
Is there a way to convert MQL5 Struct to uchar[] and back? Thanks
 
Faisal Mahmood: Is there a way to convert MQL5 Struct to uchar[] and back? Thanks

Use a "union" to overlay the structure on a uchar array, so that you can interchange between the two.

PS! You posted in the wrong section! Hopefully the admins will move it out of this reserved section. (Moved to General)

Documentation on MQL5: Language Basics / Data Types / Structures, Classes and Interfaces
Documentation on MQL5: Language Basics / Data Types / Structures, Classes and Interfaces
  • www.mql5.com
Structures, Classes and Interfaces - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
What about StructToCharArray, wouldn't that do the job as well?


 
Dominik Christian Egert #:
What about StructToCharArray, wouldn't that do the job as well?


Thanks for the suggestion. StructToCharArray copies the data and not as performant. It would have been good if we could somehow cast it via pointer address or something similar to that.

 
Faisal Mahmood #:

Thanks for the suggestion. StructToCharArray copies the data and not as performant. It would have been good if we could somehow cast it via pointer address or something similar to that.

Why don't you go with Fernando's suggestion?

Something like this: 

struct structure
   {
   uint int1;
   uint int2;
   uint int3;
   };

union tobytearray
   {
   structure integer;
   uchar     byte[12];
   };
 
Laszlo Tormasi #:

Why don't you go with Fernando's suggestion?

Something like this: 

Nice to see ways to interchange between struct and uchar[] via Unions.

Would it work for arrays of structs and then arrays of bytes (assume need to use 2 dimensional array, is there a way to have 1 dimensional array in MQL5)?

 
bot #: Nice to see ways to interchange between struct and uchar[] via Unions. Would it work for arrays of structs and then arrays of bytes (assume need to use 2 dimensional array, is there a way to have 1 dimensional array in MQL5)?

Why don't you just try it for yourself and see? One learns best by "doing"!

Also, there is something called "documentation" and it is quite extensive and available Online and Offline both in CHM and PDF formats.

There are also numerous Articles as well examples of code in the CodeBase on various things! Run a Search and see what you can find.