gpt4 book ai didi

c++ - 建立一个二进制字符串c++

转载 作者:搜寻专家 更新时间:2023-10-31 00:42:36 25 4
gpt4 key购买 nike

如何在 C++ 中构建二进制字符串?

我想用这种格式:

  std::string str = signed long (4bytes) "fill out zeros" 0x000 (8bytes) signed long (4bytes)

注意我不想要可见的二进制表示,而是字符串中的实际二进制数据

最佳答案

针对您的具体应用:

std::string data(12, 0);  // construct string of 12 null-bytes

int32_t x, y; // populate

char const * const p = reinterpret_cast<char const *>(&x);
char const * const q = reinterpret_cast<char const *>(&y);

std::copy(p, p + 4, data.begin() );
std::copy(q, q + 4, data.begin() + 8);

关于c++ - 建立一个二进制字符串c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11820378/

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