我如何将插播向量重新转换为int?

问题描述 投票:0回答:1

我有一个向量:std::vector<int8_t> src = { 0x0, 0x0, 0x27, 0x10 };

如何将pun设为int以便使用reinterpret_cast获得10.000?

我对此不感兴趣:(int32_t)((src[offset] << 24) | (src[offset+ 1] << 16) | (src[offset+ 2] << 8) | (src[offset+ 3]));

c++ casting reinterpret-cast
1个回答
0
投票
#include <iostream>
#include <numeric>
#include <vector>

int main() {
  std::vector<int8_t> src = { 0x0, 0x0, 0x27, 0x10 };
  std::cout <<
    std::accumulate(src.begin(), src.end(),  // or src.begin() + sizeof(int32_t)
       0, [](const int32_t &a, const int32_t &b){ return a * 256 + b; });
  std::cout << std::endl;
  return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.