如何使用QByteArray读取16位整数
How to read 16 bit integer using 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位i。。在0=-344(//二进制//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);
}
从QByteArray中获取constData(或data)指针,并强制转换为qint16:
QByteArray fileBuf;
const char * data=fileBuf.constData();
const qint16 * data16=reinterpret_cast<const qint16 *>(data);
int len=fileBuf.size()/(sizeof(qint16)); //Get the new len, since size() returns the number of
//bytes, and not the number of 16bit words.
//Iterate over all the words:
for (int i=0;i<len;++i) {
//qint16 z=*data16; //Get the value
//data16++; //Update the pointer to point to the next value
//EDIT: Both lines above can be replaced with:
qint16 z=data16[i]
//Use z....
qDebug()<< i<<" is= "<<z;
}
相关文章:
- AVX2 整数乘以有符号 8 位元素,产生有符号 16 位结果?
- 16 位系统中的程序如何访问大于 65535 的整数,但不能访问地址
- MongoDB C++ 驱动程序 - 8 位和 16 位整数?
- 对 32 位整数进行哈希处理比对 3 个 16 位整数的哈希进行按位运算慢?
- 有没有办法让32位C 编译器遵循16位整数促销规则
- 生成伪随机16位整数
- 使用 SSE 内部函数存储四个 16 位整数
- 无法在OpenGL碎片着色器中正确检索16位整数
- 有效地将 16 位整数哈希为 256 位空间
- 从二进制文件 c++ 读取 16 位整数
- 需要一个支持 16 位整数 (c++) 的免费线程安全矩阵数学库
- Qt 5 带有 QOpenGLTexture 和 16 位整数图像
- 将 8 位整数放入 16 位整数数组的最快方法是什么
- 将 RGB 存储在无符号的 16 位整数中
- 将 16 位整数转换为字符数组?(C++)
- 如何使用QByteArray读取16位整数
- 将一个16位整数复制到一个两字节数组
- 如何将16位整数写入文件
- 将typeid().hash_code()存储为16位整数
- 如何从QFile中读取有符号的16位整数