Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将int打包成字符串 我试图把这个序列从Python翻译成C++。 bytesString = struct.pack('!l', value)_Python_C++_Pack - Fatal编程技术网

将int打包成字符串 我试图把这个序列从Python翻译成C++。 bytesString = struct.pack('!l', value)

将int打包成字符串 我试图把这个序列从Python翻译成C++。 bytesString = struct.pack('!l', value),python,c++,pack,Python,C++,Pack,如何使用字节移位将整数值打包到std::string中?易于维护的方法(不是endian不可知) 将原语类型的值“编码”为字节序列的典型方法是使用简单的std::copy: #include <string> #include <iostream> #include <iomanip> template <typename T> std::string pack(const T val) { std::string bytes(sizeof

如何使用字节移位将整数值打包到std::string中?

易于维护的方法(不是endian不可知) 将原语类型的值“编码”为字节序列的典型方法是使用简单的
std::copy

#include <string>
#include <iostream>
#include <iomanip>

template <typename T>
std::string pack(const T val)
{
   std::string bytes(sizeof(T), '\0');
   std::copy(
      reinterpret_cast<const char*>(&val),
      reinterpret_cast<const char*>(&val) + sizeof(T),
      bytes.begin()
   );
   return bytes;
}

int main()
{
   int x = 42;
   std::string bytes{pack(x)};

   std::cout << std::noshowbase << std::hex << std::setfill('0');
   for (auto c : bytes)
      std::cout << "0x" << std::setw(2) << +c << ' ';

   // ^ may need tweaking for values above 127; not sure
}

// On my little-endian system with 32-bit int:
//  "0x2a 0x00 0x00 0x00"
(简单地重新解释整个值(而不是复制)的解决方案可能存在对齐和别名问题。)


优化方法(网络字节顺序)

如果你的代码处于一个严格的循环中,你不想使用二进制数转换和拷贝,那么你可以考虑一个循环移位:

#include <string>
#include <climits>
#include <iostream>
#include <iomanip>

template <typename T>
std::string pack_in_network_order(const T val)
{
   const size_t NBYTES = sizeof(T);
   std::string bytes(NBYTES, '\0');

   for (size_t i = 0; i < NBYTES; i++)
      bytes[NBYTES - 1 - i] = (val >> (i * CHAR_BIT)) & 0xFF;

   return bytes;
}

int main()
{
   int x = 42;
   std::string bytes{pack_in_network_order(x)};

   std::cout << std::noshowbase << std::hex << std::setfill('0');
   for (auto c : bytes)
      std::cout << "0x" << std::setw(2) << +c << ' ';
}

// On my system with 32-bit int:
//  "0x00 0x00 0x00 0x2a"
#包括
#包括
#包括
#包括
模板
std::网络顺序中的字符串包(const T val)
{
常量大小n字节=大小f(t);
std::字符串字节(n字节,'\0');
对于(大小i=0;i>(i*字符位))&0xFF;
返回字节;
}
int main()
{
int x=42;
字符串字节{pack_in_network_order(x)};

std::我是否尝试使用字节移位来存储字符数组中的值,如缓冲区[I]=(val>>8)&0xff
#include <string>
#include <climits>
#include <iostream>
#include <iomanip>

template <typename T>
std::string pack_in_network_order(const T val)
{
   const size_t NBYTES = sizeof(T);
   std::string bytes(NBYTES, '\0');

   for (size_t i = 0; i < NBYTES; i++)
      bytes[NBYTES - 1 - i] = (val >> (i * CHAR_BIT)) & 0xFF;

   return bytes;
}

int main()
{
   int x = 42;
   std::string bytes{pack_in_network_order(x)};

   std::cout << std::noshowbase << std::hex << std::setfill('0');
   for (auto c : bytes)
      std::cout << "0x" << std::setw(2) << +c << ' ';
}

// On my system with 32-bit int:
//  "0x00 0x00 0x00 0x2a"