嘿大家這可能會變成一個簡單的愚蠢的問題,但一直讓我頭痛一陣子。我正在讀取來自命名二進制標記文件的數據,並且該代碼正在工作,除非我嘗試讀取big-endian數字。該得到的整數代碼如下所示:從文件讀取二進制給出負數
long NBTTypes::getInteger(istream &in, int num_bytes, bool isBigEndian)
{
long result = 0;
char buff[8];
//get bytes
readData(in, buff, num_bytes, isBigEndian);
//convert to integer
cout <<"Converting bytes to integer..." << endl;
result = buff[0];
cout <<"Result starts at " << result << endl;
for(int i = 1; i < num_bytes; ++i)
{
result = (result << 8) | buff[i];
cout <<"Result is now " << result << endl;
}
cout <<"Done." << endl;
return result;
}
而且READDATA功能:
void NBTTypes::readData(istream &in, char *buffer, unsigned long num_bytes, bool BE)
{
char hold;
//get data
in.read(buffer, num_bytes);
if(BE)
{
//convert to little-endian
cout <<"Converting to a little-endian number..." << endl;
for(unsigned long i = 0; i < num_bytes/2; ++i)
{
hold = buffer[i];
buffer[i] = buffer[num_bytes - i - 1];
buffer[num_bytes - i - 1] = hold;
}
cout <<"Done." << endl;
}
}
此代碼,最初的工作(給正確的正值),但現在不管是什麼原因,我得到的值無論是過度還是下降。我錯過了什麼?
號碼是多少你期待?有可能這個數字太大而不能代表那麼久?嘗試使用無符號長整型。 – TripShock
向我們展示'readData'的輸入和輸出示例,包括'isBigEndian'的設置。 –
感謝您的意見。我期望的數字是128,但是我得到了-128。二進制輸入是一個大端短(128)。返回的字節表明我應該得到正確答案 –