1

実際に Qt で QString を使用しています。簡単な機能があれば教えてください:)

私が考えているのは、バイナリ文字列をバイト単位でファイルに格納することです。

QString code = "0110010101010100111010 /*...still lots of 0s & 1s here..*/";
ofstream out(/*...init the out file here...*/);
for(; code.length() / 8 > 0; code.remove(0, 8))    //a byte is 8 bits
{
    BYTE b = QStringToByte(/*...the first 8 bits of the code left...*/);
    out.write((char *)(&b), 1);
}
/*...deal with the rest less than 8 bits here...*/

QStringToByte() 関数をどのように記述すればよいですか?

BYTE QStringToByte(QString s)    //s is 8 bits
{
    //?????
}

お返事ありがとうございます。

4

2 に答える 2

1

QString には素敵なtoIntメソッドがあり、オプションでベースをパラメーターとして取ります (この場合はベース 2)。8 文字を取り除いて新しい QString を作成し、str.toInt( &somebool, 2 ).

エラーチェックを行わないと、おそらく次のようになります。

BYTE QStringToByte(QString s)    //s is 8 bits
{
  bool ok;
  return (BYTE)(s.left( 8 ).toInt( &ok, 2 ));
}

(私の言葉を信じないでください、私の人生でQtに一行も書いたことはありません)

于 2012-04-06T00:00:37.747 に答える
0

boost::dynamic_bitsetファイルにビットを書き込もうとするかもしれません。

void write_to_file( std::ofstream& fp, const boost::dynamic_bitset<boost::uint8_t>& bits )
{
     std::ostream_iterator<boost::uint8_t> osit(fp);
     boost::to_block_range(bits, osit);
}
于 2012-04-06T00:03:02.117 に答える