LoginSignup
1
0

More than 5 years have passed since last update.

C++でSliceをやってみる

Last updated at Posted at 2017-05-20

某通信処理で64KBで分割したchunkを作る予定なのでSliceを試してみた。

Install

$ sudo apt install libboost-dev

評価コード

#include <iostream>
#include <iterator> 
#include <fstream>
#include <boost/range/adaptor/sliced.hpp>
#include <boost/range/algorithm/copy.hpp>

int main(){
  std::ifstream input( "1.jpg", std::ios::binary );
  std::vector<char> buffer((
    std::istreambuf_iterator<char>(input)), 
    (std::istreambuf_iterator<char>()));

  for (size_t i=2; i<10; i++){
    std::cout << std::hex << (int)(unsigned char)buffer[i];
  }
  std::cout << std::endl;

  std::string str;
  boost::copy(
    buffer | boost::adaptors::sliced(2,10),
    std::back_inserter(str));

  for (int x = 0; x < str.size(); x++)
  {
    std::cout << (int)(unsigned char)str[x];
  }
  std::cout << std::endl;

  // std::cout << str << std::endl;
  // std::ofstream ofs("out.bin", std::ios::binary);
  // ofs.write(&str[0], str.size());
}

出力

ffe00104a464946
ffe00104a464946

バイナリ

$ od -x 1.jpg | head -n 1
0000000 d8ff e0ff 1000 464a 4649 0100 0001 0100
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0