簡體   English   中英

將成員變量復制到字節向量中

[英]copy member variable into byte vector

我想將64位成員變量逐字節復制到向量中。

請避免告訴我使用位操作提取每個字節,然后將其復制到向量中。

我想一行完成。

我使用memcpy和copy方法,但是它們都失敗了。

這是示例代碼:

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

using namespace std;

class A {
 public:
  A()
  : eight_bytes_data(0x1234567812345678) {
  }

  void Test() {
    vector<uint8_t> data_out;
    data_out.reserve(8);
    memcpy(data_out.data(),
           &eight_bytes_data,
           8);
    cerr << "[Test]" << data_out.size() << endl;
  }

  void Test2() {
    vector<uint8_t> data_out;
    data_out.reserve(8);
    copy(&eight_bytes_data,
         (&eight_bytes_data) + 8,
         back_inserter(data_out));
    cerr << "[Test2]" << data_out.size() << endl;
    for (auto value : data_out) {
      cerr << hex << value << endl;
    }
  }

 private:
  uint64_t eight_bytes_data;
};

int main() {
  A a;
  a.Test();
  a.Test2();
  return 0;
}

如果要使用其他類型結構的字節,則可以使用char *操作每個字節:

void Test3()
{
    vector<uint8_t> data_out;
    char* pbyte = (char*)&eight_bytes_data;

    for(int i = 0; i < sizeof(eight_bytes_data); ++i)
    {
        data_out.push_back(pbyte[i]);
    }

    cerr << "[Test]" << data_out.size() << endl;

}

不幸的是,您要求提供一種解決方案,但我認為這是不可行的。

正如其他人已經指出的那樣,您錯在哪里,有一個單一的解決方案令人難以置信。

首先,您需要確保向量的大小足以接收8個字節。 像這樣:

data_out.resize(8);

您可以執行reinterpret_cast強制編譯器將向量中的這8個字節解釋為8個字節的唯一類型,然后執行復制操作

*(reinterpret_cast<uint64_t*>(data_out.data())) = eight_bytes_data;

我無法弄清楚出現問題的所有可能性。 因此,使用風險自負。

如果您對更通用的版本感興趣:

namespace detail
{
    template<typename Byte, typename T>
    struct Impl
    {
        static std::vector<Byte> impl(const T& data)
        {
                std::vector<Byte> bytes;
                bytes.resize(sizeof(T)/sizeof(Byte));
                *(T*)bytes.data() = data;
                return bytes;
        }
    };

    template<typename T>
    struct Impl<bool, T>
    {
        static std::vector<bool> impl(const T& data)
        {
            std::bitset<sizeof(T)*8> bits(data);
            std::string string = bits.to_string();
            std::vector<bool> vector;
            for(const auto& x : string)
                vector.push_back(x - '0');
            return vector;
        }
    };
}

template<typename Byte = uint8_t,
         typename T>
std::vector<Byte> data_to_bytes(const T& data)
{
    return detail::Impl<Byte,T>::impl(data);
}

int main()
{
    uint64_t test = 0x1111222233334444ull;

    for(auto x : data_to_bytes<bool>(test))
        std::cout << std::hex << uintmax_t(x) << " ";
    std::cout << std::endl << std::endl;

    for(auto x : data_to_bytes(test))
        std::cout << std::hex << uintmax_t(x) << " ";
    std::cout << std::endl << std::endl;

    for(auto x : data_to_bytes<uint16_t>(test))
        std::cout << std::hex << uintmax_t(x) << " ";
    std::cout << std::endl << std::endl;

    for(auto x : data_to_bytes<uint32_t>(test))
        std::cout << std::hex << uintmax_t(x) << " ";
    std::cout << std::endl << std::endl;

    for(auto x : data_to_bytes<uint64_t>(test))
        std::cout << std::hex << uintmax_t(x) << " ";
    std::cout << std::endl << std::endl;

}

輸出:

0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1
0 0 0 1 0 0 0 1 0 0 0 1 0 0

44 44 33 33 22 22 11 11

4444 3333 2222 1111

33334444 11112222

1111222233334444

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM