'How to get a python list that is converted to a string, assigned to attributes in a struct in C++
Okay I am editing my original post. Apologies for not being clear enough earlier. I am relatively new to the developer role, specifically C,C++,Python and Embedded Linux.
There is a python list plist = [2,434]
This data is sent to another program written in C++ using socket programming.
plist = str(plist)
sock = socket.socket(socket.AF_INET, socket.SOCK_STRREAM)
sock.connect(('localhost',12345))
sock.send(plist.encode('utf-8'))
sock.close()
Now this data is received by the C++ program as a string. I just made a sample string as follows to give you an idea what I am getting at the C++ end.
const char* construct= "[2,434]";
Now I am trying to assign the two numbers, 2 and 434, to playerType and playerID respectively, which are part of the following structure called PLAYER_HEADER.
typedef enum
{
START,
STOP,
PAUSE,
RECORD
}PLAYER_TYPE;
typedef struct
{
PLAYER_TYPE playerType;
unsigned long playerID;
}PLAYER_HEADER;
So far I've tried this approach, where I am typecasting the string to a struct type.
PLAYER_HEADER* p= (PLAYER_HEADER* ) construct;
player = p->playerType;
cout<<(char)player<<endl;
But this prints only '[' which is the first character of [2,434]
I would like to know why this is happening. Is there a way I can get the rest of the data?
I get that I have typecasted as a character.
My end goal is to assign the two numbers to playerType and playerID .
Is there any other way I can do this? Or is there a concept in C++ I need too understand to solve this problem?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
