rcplusplus
rcplusplus

Reputation: 2877

Encoding an std::string in C++

I wrote a simple Person class with the following members:

std::string _fname;
std::string _lname;
int _age;

I plan to write an AddressBook class later on with a vector<Person> _contacts as a member variable. For now, I decided that I would serialize AddressBook would be to write an encoded version of each contact on a separate line in the file which would be opened in binary mode. It'll basically be a string which I'll parse later to get the values, somewhere along these lines: "John~Doe~42"

I want to encode this string however, because if my Person class gets many new member variables, this string can get quite long. I was hoping that it could be encoded as hex representation, but it seems highly unlikely, so I need to ask, are there any other ways to encode an std::string to make it non-decipherable by humans, if not compact?

Upvotes: 0

Views: 418

Answers (4)

user491135
user491135

Reputation:

What about using sqlite? By using that you don't need to serialize data, etc etc. You just create a file that is binary and you access as a regular database.

And in long term, you can scale your application by connecting to another database without having to rewrite everything.

Here you can find a nice intro.

Upvotes: 1

selbie
selbie

Reputation: 104549

You'll be in a better place by using XML as your file format. That way if you decided to add an optional "middle name" to your Person class in version 2, the address book content can be backwards and forward compatible with older and newer versions of your code.

Serialize your data out as XML, then compress for the win. There's plenty of good compression libraries out there that are mostly free and include source. zlib works well.

There's also the Boost Serialization library to look at.

Plenty of XML libraries as well. Bing for it.

Upvotes: 0

Loki Astari
Loki Astari

Reputation: 264471

I want to encode this string however, are there any other ways to encode an std::string to make it non-decipherable by humans

By humans. Which humans?

Personally I classify them as:

  • "My Stupid Brother"
  • "My Nosy but Smart Mum"
  • "A competing Company"
  • "The government".

Encryption to beat people:

  • Brother: (add 'N' to each character) (subtract 'N' to decode)
  • Mother: (XOR a key)
  • Company: Noting short of a real encryption package (eg SHA-512)
  • Government: Yea right.
    If they want to get something from me I have no chance.
    So I hire a professional company to handle it.

For now, I decided that I would serialize AddressBook would be to write an encoded version of each contact on a separate line in the file which would be opened in binary mode.

Binary/Text mode there is no real difference. Binary mode just means that '\n' is not converted to/from the End of Line Sequence.

It'll basically be a string which I'll parse later to get the values, somewhere along these lines: "John~Doe~42"

Sure, but I would use ' ' (space) as the separator (as the stream library already uses as the default seporator). This will make the code much easier to write. As previously noted strings need to be encoded with their length (unless you have fixed length strings).

because if my Person class gets many new member variables, this string can get quite long.

If you are planning to encode more information latter then I would also add a version field. Thus you read the version number first, this tells you what other fields to expect next.

I was hoping that it could be encoded as hex representation

This does not make sense.
Everything is just a number.

if not compact?

Sure you can compress the data.
Look up compression algorithms.

Upvotes: 4

George
George

Reputation: 4674

You can try XOR, or add ASCII value to offset the characters. Like transform 'a' to 'r', 'b' to 's'. Method like this is not super strong, but people can not look at it and read it directly. But for anything serious, like Social security number, credit card number, you want to use something more serious, and be more careful about it.

Upvotes: 0

Related Questions