我有20byte二进制char数组。我想分为3部分:4byte,8byte,8byte。我像下面这样实现它。它有效,但似乎我可以使用缓冲流。我想知道如何使用它。

现在

void main()
{
    // _data is 20byte binary char array. 0000000000000000000000000000000000000000000001111001110001111111001110000010110000001011101101000000000000000000000000000000000000000000000000000000000000000001

    // strA (4 byte)
    string strA;
    for (std::size_t i = 0; i < 4; ++i) {
        strA += bitset<8>(_data.c_str()[i]).to_string();
    }

    cout << strA << endl; // 00000000000000000000000000000000

    // strB (8 byte)
    string strB;
    for (std::size_t i = 4; i < 12; ++i) {
        strB += bitset<8>(_data.c_str()[i]).to_string();
    }

    cout << strB << endl; // 0000000000000111100111000111111100111000001011000000101110110100

    // strC (8 byte)
    string strC;
    for (std::size_t i = 12; i < 20; ++i) {
        strC += bitset<8>(_data.c_str()[i]).to_string();
    }

    cout << strC << endl; // 0000000000000000000000000000000000000000000000000000000000000001
}

期望

我想这样实现。
void main()
{
    stringstream ss = _data;
    strA = ss.pop(4);
    strB = ss.pop(8);
    strC = ss.pop(8);
}

更新1

感谢你们。我正在尝试您给我的所有答案。我是C++的新手,所以花时间来理解它。以下是Anders K的作品。
struct S { char four[4]; char eight1[8]; char eight2[8]; };
struct S *p = reinterpret_cast<S*>(&_data);
cout << p->four << endl; // => Output "(" I think I can find way to output

更新2

它使用string::substr起作用。谢谢扎基尔。
int main()
{
    // I don't know how to change to string value in smart way..
    string str;
    for (std::size_t i = 0; i < _data.size(); ++i) {
        str += bitset<8>(_data.c_str()[i]).to_string();
    }

    cout << str << endl; // 0000000000000000000000000000000000000000000001111001110001111111001110000010110000001011101101000000000000000000000000000000000000000000000000000000000000000001

    std::string d = str; // Your binary stream goes here
    int lenA = (4*8);  // First  4 Bytes
    int lenB = (8*8);  // Second 8 Bytes
    int lenC = (8*8);  // Last   8 Bytes

    std::string strA = d.substr(0,    lenA);
    std::string strB = d.substr(lenA + 1, lenB - 1);
    std::string strC = d.substr(lenA + lenB + 1, lenC - 1);

    cout << strA << endl; // 00000000000000000000000000000000
    cout << strB << endl; // 000000000000111100111000111111100111000001011000000101110110100
    cout << strC << endl; // 000000000000000000000000000000000000000000000000000000000000001
}

更新3

尝试Scheff的方法时出现错误。这是我的错,我想我可以解决的。而且我认为我应该重新考虑_data的类型。
int main
{
    const char data = _data;
    const char *iter = data;
    string strA = pop(iter, 4);
    string strB = pop(iter, 8);
    string strC = pop(iter, 8);
    cout << "strA: '" << strA << "'" << endl;
    cout << "strB: '" << strB << "'" << endl;
    cout << "strC: '" << strC << "'" << endl;
}

产生错误讯息
error: no viable conversion from 'string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') to
  'const char'
const char data = _data;

最佳答案

我可以使用string::substr向您推荐一个非常的简单替代方案

#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string _data="00010001001000100011001101000100\
0101010101100110011101111000100010011001101010101011101111001100\
1101110111101110111111111101111010101101101111101110111100000000";

  int lenA = (4*8);  //First 4 Bytes
  int lenB = (8*8);  //Second 8 Bytes
  int lenC = (16*8); //Last 16 Bytes

  string strA = _data.substr(0,    lenA - 1);
  string strB = _data.substr(lenA, lenB - 1);
  string strC = _data.substr(lenB, lenC - 1);

  std::cout << "strA: " << strA <<  endl;
  std::cout << "strB: " << strB <<  endl;
  std::cout << "strC: " << strC <<  endl;


  return 0;
}

这很简洁,但是可以完成您的工作!
示范here

输出:-
strA: 0001000100100010001100110100010
strB: 010101010110011001110111100010001001100110101010101110111100110
strC: 100110011010101010111011110011001101110111101110111111111101111010101101101111101110111100000000

关于c++ - 将二进制char数组转换为stringstream并从缓冲区弹出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44605442/

10-10 04:17