[英]How to read signed 16bit integers from QFile?
我想從Qt的音頻文件中讀取16位整數樣本值。
char tempbytes[3]; ///
int sampleposition;
int sample;
QFile file(sourcepath);
if (! file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
sampleposition = 120
file.seek(sampleposition );
file.read(tempbytes, 2);
現在我有一個char數組,但我想將其轉換為16位帶符號整數值。 波形文件中的字節順序為littleendian。
我已經嘗試過:
sample = qFromLittleEndian<qint16>((uchar*)tempbytes);
這適用於某些樣本,但有些樣本被錯誤的值完全誤解了。 我將時間信號以大膽的方式進行了比較。
有沒有更好的辦法?
不管這是一個“更好”的解決方案,我都發現在這種情況下使用QDataStream
更為簡單。 它特別具有讀取16位整數的重載。
這是一個非常基本的示例:
// Open the file and create our data stream
QFile file("filename");
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
in.setByteOrder(QDataStream::LittleEndian);
// Read a 16-bit sample
qint16 sample;
in >> sample
源文件不是文本文件,因此請不要這樣對待。 打開它的正確方法是:
if (! file.open(QIODevice::ReadOnly)) return;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.