Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.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
C++ 如何从c+中的整数缓冲区中提取4个字节的整数+;_C++_C - Fatal编程技术网

C++ 如何从c+中的整数缓冲区中提取4个字节的整数+;

C++ 如何从c+中的整数缓冲区中提取4个字节的整数+;,c++,c,C++,C,我有整数的缓冲区/有效负载,其中每4个字节是一个我需要提取的字段。 我有一个指向缓冲区的指针。现在我必须提取4个字节并将其分配给变量,直到到达缓冲区的末尾。 指针是指向buf的uint8_t指针,buf是一个缓冲区对象,其详细信息被封装。 做这件事的最好和最优雅的方式是什么? 我用C++编写代码。如有任何建议,我们将不胜感激 转换为4字节整数和索引的数组。转换为4字节整数和索引的数组。如果缓冲区中的整数是字对齐的,您可以尝试: const char* ptr; // this points to

我有整数的缓冲区/有效负载,其中每4个字节是一个我需要提取的字段。 我有一个指向缓冲区的指针。现在我必须提取4个字节并将其分配给变量,直到到达缓冲区的末尾。 指针是指向buf的uint8_t指针,buf是一个缓冲区对象,其详细信息被封装。 做这件事的最好和最优雅的方式是什么?
我用C++编写代码。如有任何建议,我们将不胜感激

转换为4字节整数和索引的数组。

转换为4字节整数和索引的数组。

如果缓冲区中的整数是字对齐的,您可以尝试:

const char* ptr; // this points to a position in a buffer
int value = reinterpret_cast<int*>(ptr);
const char*ptr;//这指向缓冲区中的一个位置
int值=重新解释铸件(ptr);
否则,可能更安全、更可取:

const char* ptr;
int value;
std::copy(ptr, ptr+sizeof(int), reinterpret_cast<char*>(value));
const char*ptr;
int值;
复制(ptr,ptr+sizeof(int),reinterpret_cast(value));

顺便说一句:请确保您没有endianess问题(即,您的机器和保存这些Int的机器必须具有相同的endianess才能工作,否则您需要进行补偿)。你依赖于C++的具体实现。

如果缓冲区中的整数是字对齐的,可以尝试:

const char* ptr; // this points to a position in a buffer
int value = reinterpret_cast<int*>(ptr);
const char*ptr;//这指向缓冲区中的一个位置
int值=重新解释铸件(ptr);
否则,可能更安全、更可取:

const char* ptr;
int value;
std::copy(ptr, ptr+sizeof(int), reinterpret_cast<char*>(value));
const char*ptr;
int值;
复制(ptr,ptr+sizeof(int),reinterpret_cast(value));

顺便说一句:请确保您没有endianess问题(即,您的机器和保存这些Int的机器必须具有相同的endianess才能工作,否则您需要进行补偿)。你依赖于C++的具体实现。

< P>一次读取一个字节,将它们移到int中,或者使用MeMCPY将字节复制到正确类型的变量中。复制也是可以接受的,我不确定它的别名规则


按建议对数组进行强制转换可能会破坏严格的别名规则,因此无法保证正常工作,并且可能是未定义的行为。

一次读取一个字节,然后将其移位/添加到int中,或者使用memcpy将字节复制到正确类型的变量中。复制也是可以接受的,我不确定它的别名规则


按建议对数组进行强制转换可能会违反严格的别名规则,因此无法保证正常工作,并且可能是未定义的行为。

您可以使用C代码按如下方式进行转换:

int i = 0;
int value;
while(i < buffersize)
{
  value = 0;
  memcpy(&value, buffer, sizeof(int));
  //if the buffer is from network, then you may have to do a conversion from network order to host order here
  printf("%d", value);
  buffer = buffer + sizeof(int);
  i = i + sizeof(int);
}
inti=0;
int值;
while(i
您可以使用C代码按如下方式执行:

int i = 0;
int value;
while(i < buffersize)
{
  value = 0;
  memcpy(&value, buffer, sizeof(int));
  //if the buffer is from network, then you may have to do a conversion from network order to host order here
  printf("%d", value);
  buffer = buffer + sizeof(int);
  i = i + sizeof(int);
}
inti=0;
int值;
while(i
断言(buf_字节%4==0);
std::矢量数(buf_字节/4);
memcpy(&numbers[0],buf,buf_字节);
如果(需要字节交换)
std::for_each(numbers.begin()、numbers.end()、[](uint32_t&n){ntohl(n);});
断言(buf_字节%4==0);
std::矢量数(buf_字节/4);
memcpy(&numbers[0],buf,buf_字节);
如果(需要字节交换)
std::for_each(numbers.begin()、numbers.end()、[](uint32_t&n){ntohl(n);});

我建议逐个读取字节,然后“手动”组合数字。这要求您清楚预期的结束,这是一件好事。由于从缓冲区读取的都是字节,因此它还可以使代码满足任何对齐要求

uint32_t extract_uint32_be(const uint8_t *buffer)
{
  const uint8_t b0 = buffer[0], b1 = buffer[1], b2 = buffer[2], b3 = buffer[3];

  return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
}
uint32\u t提取uint32\u be(常量uint8\u t*缓冲区)
{
const uint8_t b0=缓冲区[0],b1=缓冲区[1],b2=缓冲区[2],b3=缓冲区[3];

return(b0我建议逐个读取字节,并“手动”组合数字。这要求您清楚预期的endianness,这是一件好事。这也使代码符合任何对齐要求,因为您从缓冲区读取的都是字节

uint32_t extract_uint32_be(const uint8_t *buffer)
{
  const uint8_t b0 = buffer[0], b1 = buffer[1], b2 = buffer[2], b3 = buffer[3];

  return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
}
uint32\u t提取uint32\u be(常量uint8\u t*缓冲区)
{
const uint8_t b0=缓冲区[0],b1=缓冲区[1],b2=缓冲区[2],b3=缓冲区[3];

返回(b0您可以使用C代码按如下方式执行:

int i = 0;
int value;
while(i < buffersize)
{
  value = 0;
  memcpy(&value, buffer, sizeof(int));
  //if the buffer is from network, then you may have to do a conversion from network order to host order here
  printf("%d", value);
  buffer = buffer + sizeof(int);
  i = i + sizeof(int);
}
我指向要从中读取4个字节的缓冲区

 read = (UInt32)((UInt8*)p_msg)[i++] << 24;
 read |= (UInt32)((UInt8*)p_msg)[i++] << 16;
 read |= (UInt32)((UInt8*)p_msg)[i++] << 8;
 read |= (UInt32)((UInt8*)p_msg)[i++];

read=(UInt32)((UInt8*)p_msg)[i++]您可以使用C代码如下所示:

int i = 0;
int value;
while(i < buffersize)
{
  value = 0;
  memcpy(&value, buffer, sizeof(int));
  //if the buffer is from network, then you may have to do a conversion from network order to host order here
  printf("%d", value);
  buffer = buffer + sizeof(int);
  i = i + sizeof(int);
}
我指向要从中读取4个字节的缓冲区

 read = (UInt32)((UInt8*)p_msg)[i++] << 24;
 read |= (UInt32)((UInt8*)p_msg)[i++] << 16;
 read |= (UInt32)((UInt8*)p_msg)[i++] << 8;
 read |= (UInt32)((UInt8*)p_msg)[i++];

read=(UInt32)((UInt8*)p_msg)[i++]你的意思是你有一个整数数组或要转换为整数的字节数组?一个指向buf的uint8_t指针。buf一个缓冲区对象,而不是一个数组它来自调用者。我没有控制权。它被封装了。我所拥有的只是指向它的指针指针指针是指向buf的uint8_t指针,而buf是一个缓冲区对象,它的细节没有被封装确定这意味着什么。如果你有一个指向buf的指针,那么它就不是真正封装的,是吗?你有没有尝试编写一个解决方案?请发表你的努力。你的意思是你有一个整数数组或要转换为整数的字节数组?一个指向buf的uint8\u t指针。buf一个缓冲区对象,而不是来自调用者的数组。I没有控制权。它是封装的。我所拥有的只是指向它的指针。指针是指向buf的uint8\u t指针,buf是一个缓冲区对象,其详细信息被封装。不确定这意味着什么。如果您有指向
uint8\u t
的指针,那么它不是真正封装的吗?您是否尝试过编写解决方案?请发布您的成果。After复制到一个适当对齐的内存缓冲区。复制到一个适当的