Encoding a struct?

I need to send a struct, that includes several integers and string (so far) over the network, but I'm not really happy how it works

I have a function that puts everything in a string, which I send over to the server and a function that decodes that string and puts everything in place, but as you might see from the code, the string that i'm sending thru the network is just a simple string that can be read by anyone, so is there a way to somehow encode the struct to a "unreadable" form (maybe not a string) but still decodable?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
struct Character
{
	int iRace, iClass, iLevel;
	string name;

	string encode()
	{
		string str = "";
		str += to_string(iRace);
		str += ",";
		str += to_string(iClass);
		str += ",";
		str += to_string(iLevel);
		str += ",";
		str += name;
		return str;
	}

	void decode(string str)
	{
		char comma;
		stringstream ss;
		ss.str(str);
		ss >> iRace;
		ss >> comma;
		ss >> iClass;
		ss >> comma;
		ss >> iLevel;
		ss >> comma;
		ss >> name;
	}
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	Character player;
	player.iRace = 1;
	player.iClass = 9;
	player.iLevel = 20;
	player.name = "CharacterName";

	string str = player.encode();

	Character player2;
	player2.decode(str);

	cout << str << endl; //string looks as "1,9,20,CharacterName"
	cout << player2.iRace << endl << player2.iClass << endl << player2.iLevel << endl << player2.name; //decode works fine
	cin.get();
}


Last edited on
you should do some search about sockets depending on your connection protocol.
((x ^ k) ^ k) == x, so a quick way to obfuscate a bitstring is to bitwise-XOR it by some arbitrary constant. It's not encryption, obviously, but it makes it so a human can't simply look at the stream to know what's being transmitted.
You're probably better off encrypting the connection rather than the individual data bits within it.
@Helios thanks, will try that out
@dhayden how do I do that?
Topic archived. No new replies allowed.