Convert double values to uchar[]?

 
I need to stream tick for all pairs from my EA to my external python application via zmq. It currently sends string data but it's uses more CPU than I need it to so I'm considering sending the data in the binary format.

The data being sent is mostly double values, with some strings. It seems the string parts can be converted to char array with StringToCharArray but there's no double equivalent of this.

An example of data being transmitted is as follows:

1 GBPJPY 1572224399881 132.33 132.533 157222439000 131.233 131.521 131.199 131.236 131.4 131.211 EURJPY 1572224399881 132.33 132.533 157222439000 131.233 131.521 131.199 131.236 131.4 131.211 AUDJPY 1572224399881 132.33 132.533 157222439000 131.233 131.521 131.199 131.236 131.4 131.211

A double in MQL4 is 8 bytes and a uchar is 1 byte, so I'm wondering if it would be possible to convert a double value to 8 uchars, and put all above string and double values into a single raw uchars  array that I can send.

 
sjl9203:
I need to stream tick for all pairs from my EA to my external python application via zmq. It currently sends string data but it's uses more CPU than I need it to so I'm considering sending the data in the binary format.

The data being sent is mostly double values, with some strings. It seems the string parts can be converted to char array with StringToCharArray but there's no double equivalent of this.

An example of data being transmitted is as follows:


A double in MQL4 is 8 bytes and a uchar is 1 byte, so I'm wondering if it would be possible to convert a double value to 8 uchars, and put all above string and double values into a single raw uchars  array that I can send.

I'd also like to learn about that, but I'd convert the double to string, then to char array.
 
Nelson Wanyama:
I'd also like to learn about that, but I'd convert the double to string, then to char array.

I found the answer: Use union.

If you convert it to string then to char arr then you'll unnecessarily increase the number of bytes. Easy to do but not optimal, at least for my purpose.

 
union UDblBuffer
  {
   uchar buffer[24];
   double doubles[3];
  };

  UDblBuffer u;
  u.doubles[0]=1;
  u.doubles[1]=2;
  u.doubles[2]=3;

Yes, union, like so maybe.

 
sjl9203:
I need to stream tick for all pairs from my EA to my external python application via zmq. It currently sends string data but it's uses more CPU than I need it to so I'm considering sending the data in the binary format.

The data being sent is mostly double values, with some strings. It seems the string parts can be converted to char array with StringToCharArray but there's no double equivalent of this.

An example of data being transmitted is as follows:


A double in MQL4 is 8 bytes and a uchar is 1 byte, so I'm wondering if it would be possible to convert a double value to 8 uchars, and put all above string and double values into a single raw uchars  array that I can send.

Use a struct to store your double values and then convert the struct to byte array with StructToCharArray.
 
Laszlo Tormasi:
Use a struct to store your double values and then convert the struct to byte array with StructToCharArray.
This is the best answer for my case. Thank you.
Reason: