二进制char数组到stringstream并从缓冲区弹出

时间:2017-06-17 13:54:49

标签: c++

我有20byte二进制字符数组。我想分成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。谢谢Zakir。

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;

2 个答案:

答案 0 :(得分:2)

无法为std::stringstream制作新方法。 (至少,我不建议这样做。)

相反,我建议让它成为一个功能。用法类似。

#include <bitset>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

string pop(istream &in, size_t n)
{
  string ret;
  while (n--) {
    unsigned char byte = (unsigned char)in.get();
    ret += bitset<8>(byte).to_string();
  }    
  return ret;
}

int main()
{
  string data(
    "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
    "\xbb\xcc\xdd\xee\xff\xde\xad\xbe\xef\x00", 20);
  istringstream in; in.str(data);
  string strA = pop(in, 4);
  string strB = pop(in, 8);
  string strC = pop(in, 8);
  cout << "strA: '" << strA << "'" << endl;
  cout << "strB: '" << strB << "'" << endl;
  cout << "strC: '" << strC << "'" << endl;
  return 0;
}

输出:

strA: '00010001001000100011001101000100'
strB: '0101010101100110011101111000100010011001101010101011101111001100'
strC: '1101110111101110111111111101111010101101101111101110111100000000'

注意:

  1. 使用std::istream使其适用于从std::istream派生的任何流。

  2. pop()中没有错误处理。因此,如果之后传递的流不是pop(),则good()的返回结果可能会出错。

  3. 顺便说一下。我同意std::stream可能过度设计&#34;的评论。因此,这里&#34;轻量级&#34;版本:

    #include <bitset>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string pop(const char *&iter, size_t n)
    {
      string ret;
      while (n--) {
        ret += bitset<8>((unsigned char)*iter++).to_string();
      } 
      return ret;
    }
    
    int main()
    {
      const char data[] =
        "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
        "\xbb\xcc\xdd\xee\xff\xde\xad\xbe\xef\x00";
      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;
      return 0;
    }
    

    输出与上面相同。

    注意:

    1. char[]char*的使用对于越界访问更加敏感。因此,必须谨慎使用。

    2. 我不太确定(unsigned char)施法是否必要。我经常看到&#34;搞笑&#34;关于charint和符号扩展的效果,我想它不会受到伤害。 (我感觉好多了。)

答案 1 :(得分:1)

我可以使用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