c++ - 如何使用 QByteArray 读取 16 位整数

标签 c++ qt file-io qbytearray

我想用 QByteArray 读取一个文件,但问题是它按字节读取,我想要 16 位整数数组。这是我的代码...

 QByteArray fileBuf;
 sprintf_s(filepath, "c:/file.bin");}
 myfile.setFileName(filepath);
 if(!myfile.open(QIODevice::ReadOnly)) return;
 fileBuf=myfile.readAll();

这是在内部查找值的测试

 qint16 z;
 for(int test =0; test < 5 ; test++)
 {
  z= (fileBuf[i]);
 qDebug()<< i<<"is="<<z;

结果:

0 is= -88 (// in binary// 1111 1111 1010 1000)
1 is= -2   (// in binary// 1111 1111 1111 1110)
2 is= -64 
3 is= -3 
4 is= 52

这些是因为 8 位数组我需要 16 位,即 .. 值在 0 = -344 (//binary//1111 11110 1010 1000)

最佳答案

QFile myfile;
myfile.setFileName("c:/file.bin");
if(!myfile.open(QIODevice::ReadOnly)) return;

QDataStream data(&myfile);
data.setByteOrder(QDataStream::LittleEndian);
QVector<qint16> result;
while(!data.atEnd()) {
    qint16 x;
    data >> x;
    result.append(x);
}

关于c++ - 如何使用 QByteArray 读取 16 位整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25802336/

相关文章:

安卓NDK : "fatal error: ' thread' file not found"

python - Qt:按下按钮之前运行脚本

c# - 从文本文件中删除一行的有效方法

c++ - 以编程方式验证 UDP 端口是否在 C/C++ 中绑定(bind)

c++ - 顺序前向选择 (SFS) 算法

qt - 如何设置 QTabBar 关闭按钮的样式

perl - 如何在 Perl 中创建二进制文件?

Java 字符串中断文件操作

c++ - 对 ostream 类对象的常量引用

python - 不同平台上按钮的顺序(QDialogBu​​ttonBox)