0

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

Pharaun
  • 61
  • 6
  • "I don't receive any byte strings from the vector" - I think that's because its first element is a zero, which is the famous NULL-terminator that terminates C-strings. And with the `reinterpret_cast` you're essentially casting it to a C-string that starts with the NULL-terminator, a.k.a. the _empty_ string. That's why you aren't getting any data – ForceBru Mar 18 '20 at 16:45
  • Does this answer your question? [Printing an array in C++?](https://stackoverflow.com/questions/1370323/printing-an-array-in-c) – Robert Andrzejuk Mar 18 '20 at 18:29
  • Also: https://stackoverflow.com/questions/60454533/how-to-print-out-2-d-array-in-matrix-format-in-a-while-loop – Robert Andrzejuk Mar 18 '20 at 18:30
  • Thank you! As I supposed the array problem was trivial and due to my lack of knowledge, but with the issue of decoding the bytes I still don't know how to proceed. – Pharaun Mar 19 '20 at 09:37
  • What is the underlying problem you are trying to solve? You're seemingly trying to communicate between processes. Maybe check this out: [Simple IPC between C++ and Python (cross platform)](https://stackoverflow.com/questions/6915191/simple-ipc-between-c-and-python-cross-platform). Nevertheless, you say your C++ program _"deposits int and float numbers and arrays of both in the standard output as byte strings"_. How will you know where one `int`/`float` begins and ends? You've just got a stream of bytes that you will need to interpret. – GordonAitchJay Mar 21 '20 at 08:52

0 Answers0