I have two programs. The first one is programmed in c++ and as a quick summary, it deposits int and float numbers and arrays of both in the standard output as byte strings.
The second one, in python, uses the subprocess module to execute the first one, capture the standard output, read the byte sequence and transform them into the original types. As both programs are too long to write them here, I copy the code from the ones I have programmed for testing.
This is the c++ one
#include <iostream>
int main (){
uint32_t valor;
uint32_t valor2;
uint32_t lista[5] = {0, 10, 20, 30, 40};
valor = 10;
valor2 = 257;
char* byteArray = reinterpret_cast<char*>(&valor);
char* byteArray2 = reinterpret_cast<char*>(&valor2);
char* byteArray3 = reinterpret_cast<char*>(&lista);
std::cout << byteArray;
std::cout << byteArray2;
std::cout << byteArray3;
return 0;
}
This is the Python 3.6 one
import subprocess
from subprocess import PIPE
import numpy as np
print ('Inicio de la prueba de comunicacion')
output = subprocess.run('./execpp', stdout=PIPE, stderr=PIPE)
print ('La cadena recibida es la siguiente: {}'.format(output.stdout))
s = output.stdout
The thing is that in c++ the uint32_t type is 4 bytes long but when I send it through cout the size changes according to the value of the variable. One byte for the 10 or two for the 257. Since I do not know the values that the program in c++ is going to send since it reads them from a binary file in which they are stored as structs, it is very difficult for me to know how to read them in python since I do not know how many bytes the next number occupies. Also, although this is probably due to my lack of knowledge of c++, I don't receive any byte strings from the vector, and I don't know why.
This is the output of the python program with that values:
Inicio de la prueba de comunicacion La cadena recibida es la siguiente: b'\n\x01\x01'
Do you know how can I solve this problem?
Thanks