gpt4 book ai didi

c++ - 在 C++ 中编码 std::string

转载 作者:太空宇宙 更新时间:2023-11-04 16:28:24 25 4
gpt4 key购买 nike

我写了一个简单的Person具有以下成员的类:

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

我打算写一个AddressBook稍后用 vector<Person> _contacts 上课作为成员变量。现在,我决定连载 AddressBook将在文件中的单独一行上写入每个联系人的编码版本,该文件将以二进制模式打开。它基本上是一个字符串,稍后我将对其进行解析以获取值,大致如下:"John~Doe~42"

但是我想对这个字符串进行编码,因为如果我的 Person类获得了许多新的成员变量,这个字符串可能会变得很长。我希望它可以被编码为十六进制表示,但它似乎不太可能,所以我想问一下,是否有任何其他方法来编码 std::string使其无法被人类破译,如果不是紧凑的话?

最佳答案

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

由人类。哪些人?

我个人将它们归类为:

  • “我的傻弟弟”
  • “我爱管闲事但很聪明的妈妈”
  • “竞争公司”
  • “政府”。

加密打人:

  • 兄弟:(每个字符加'N')(减去'N'解码)
  • 妈妈:(异或一个键)
  • 公司:注意到缺少真正的加密包(例如 SHA-512)
  • 政府:是的。
    如果他们想从我这里得到什么,我没有机会。
    所以我聘请了专业的公司来处理。

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.

二进制/文本模式没有真正的区别。二进制模式只是意味着 '\n' 不与 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"

当然可以,但我会使用 ' '(空格)作为分隔符(因为流库已经用作默认分隔符)。这将使代码更容易编写。如前所述,字符串需要根据其长度进行编码(除非您有固定长度的字符串)。

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

这没有意义。
一切都只是一个数字。

if not compact?

当然可以压缩数据。
查找压缩算法。

关于c++ - 在 C++ 中编码 std::string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9798658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com