QDataStream
a で書かれたバイナリ ファイルを aで、LittleEndian でエンコードしたものを読みたいstd::fstream
(同じプラットフォーム上で、異なる形式を持つ 1 つのデータ型の問題は問題ではありません)。
これを行うにはどうすればよいですか?私の知る限り、std::fstream
LittleEndian データを読み書きする機能が組み込まれていません。
問題を掘り下げたところ、次のことがわかりました(疑似コード):
ofstream out; //initialized to file1, ready to read/write
ifstream in; //initialized to file2; ready to read/write
QDataStream q_out; //initialized to file2; ready to read/write
int a=5, b;
//write to file1
out << a; //stored as 0x 35 00 00 00. Curiously, 0x35 is the character '5' in ASCII-code
//write to file2
q_out << a; //stored as 0x 05 00 00 00
//read from file2 the value that was written by q_out
in >> b; //will NOT give the correct result
//read as raw data
char *c = new char[4];
in.read(c, 4);
unsigned char *dst = (unsigned char *)&b;
dst[3] = c[3];
dst[2] = c[2];
dst[1] = c[1];
dst[0] = c[0];
//b==5 now
要約するとQDataStream
、 とは異なる形式でバイナリ データを書き込みますstd::fstream
。QDataStream
を使用して書き込まれたバイナリ データを読み取る簡単な方法はありstd::fstream
ますか?