简体   繁体   English

将4字节整数放入向量的前4个char元素中

[英]Put a 4 Byte Integer into the first 4 char elements of vector

I have a vector<unsigned char> and want to put a 4 byte Integer into the first 4 elements. 我有一个vector<unsigned char>并希望将4字节的Integer放入前4个元素中。 Is there a simpler way in C++ than masking like this: 在C ++中是否有比这样的掩码更简单的方法:

myVector.at(0) = myInt & 0x000000FF;
myVector.at(1) = myInt & 0x0000FF00;
myVector.at(2) = myInt & 0x00FF0000;
myVector.at(3) = myInt & 0xFF000000;

A std::vector is guaranteed to be stored as one contiguous block of data (‡) . 保证std::vector存储为一个连续的数据块(‡) Hence it is possible to treat a std::vector<unsigned char> in basically the same way as an unsigned char buffer. 因此,可以基本上以与unsigned char缓冲区相同的方式处理std::vector<unsigned char> This means, you can memcpy your data into the vector, provided you are sure it is large enough: 这意味着,你可以memcpy数据为载体,只要你相信它足够大:

#include <vector>
#include <cstring>
#include <cstdint>

int main()
{
  std::int32_t k = 1294323;
  std::vector<unsigned char> charvec;

  if (charvec.size() < sizeof(k))
    charvec.resize(sizeof(k));

  std::memcpy(charvec.data(), &k, sizeof(k));

  return 0;
}

Note: The data() function of std::vector returns a void* to the internal buffer of the vector. 注意: std::vectordata()函数将void*返回给std::vector的内部缓冲区。 It is available in C++11 – in earlier versions it is possible to use the address of the first element of the vector, &charvec[0] , instead. 它在C ++ 11中可用 - 在早期版本中,可以使用向量的第一个元素&charvec[0]的地址。

Of course this is a very unusual way of using a std::vector , and (due to the necessary resize() ) slightly dangerous. 当然这是使用std::vector一种非常不寻常的方式,并且(由于必要的resize() )有点危险。 I trust you have good reasons for wanting to do it. 我相信你有充分的理由想要这样做。


(‡) Or, as the Standard puts it: (‡)或者,正如标准所说:

(§23.3.6.1/1) [...] The elements of a vector are stored contiguously, meaning that if v is a vector<T, Allocator> where T is some type other than bool , then it obeys the identity (§23.3.6.1/ 1)[...]向量的元素是连续存储的,这意味着如果vvector<T, Allocator> ,其中T是某种类型而不是bool ,那么它就服从了身份

  &v[n] == &v[0] + n for all 0 <= n < v.size(). 

You have to binary shift your values for this to work: 你必须二进制移动你的值才能工作:

myVector.at(0) = (myInt & 0xFF);
myVector.at(1) = (myInt >> 8) & 0xFF;
myVector.at(2) = (myInt >> 16) & 0xFF;
myVector.at(3) = (myInt >> 24) & 0xFF;

Your code is wrong: 你的代码错了:

int myInt = 0x12345678;
myVector.at(0) = myInt & 0x000000FF; // puts 0x78 
myVector.at(1) = myInt & 0x0000FF00; // tries to put 0x5600 but puts 0x00
myVector.at(2) = myInt & 0x00FF0000; // tries to put 0x340000 but puts 0x00
myVector.at(3) = myInt & 0xFF000000; // tries to put 0x12000000 but puts 0x00

you can do something similar to the following: 你可以做类似以下的事情:

#include <vector>
#include <cstdio>

void
insert_int(std::vector<unsigned char>* container, int integer)
{
    char* chars = reinterpret_cast<char*>(&integer);
    container->insert(container->end(), chars, chars+sizeof(int));
}

int main(void)
{
    std::vector<unsigned char> test_vector;
    int test_int = 0x01020304;

    insert_int(&test_vector, test_int);

    return 0;
}

just remember to account for endieness. 只记得要考虑到恩赐。 My machine prints the int in reverse order. 我的机器以相反的顺序打印int。 4,3,2,1

Your solution is incorrect as you currently have it. 您的解决方案不正确,因为您目前拥有它。 Something like: 就像是:

std::vector<unsigned char> v(sizeof(int));
int myInt = 0x12345678;

for(unsigned i = 0; i < sizeof(int); ++i) {
    v[i] = myInt & 0xFF;
    myInt >>= 8;
}

Should work. 应该管用。 It's also more portable (doesn't assume int is 4 bytes). 它也更便携(不假设int是4个字节)。

Here is the most compact way: 这是最紧凑的方式:

myVector.at(0) = *((char*)&myInt+0);
myVector.at(1) = *((char*)&myInt+1);
myVector.at(2) = *((char*)&myInt+2);
myVector.at(3) = *((char*)&myInt+3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM