【发布时间】:2016-03-08 19:12:24
【问题描述】:
dataFile.bin 是一个包含 6 字节记录的二进制文件。第一个 3 每条记录的字节包含纬度,最后 3 个字节包含 经度。每个 24 位值代表弧度乘以 0X1FFFFF
这是我一直在做的一项任务。我已经好几年没学过 C++ 了,所以它花费的时间比我想象的要长方式 -_-。在谷歌搜索之后,我看到了这个对我来说很有意义的算法。
int interpret24bitAsInt32(byte[] byteArray) {
int newInt = (
((0xFF & byteArray[0]) << 16) |
((0xFF & byteArray[1]) << 8) |
(0xFF & byteArray[2])
);
if ((newInt & 0x00800000) > 0) {
newInt |= 0xFF000000;
} else {
newInt &= 0x00FFFFFF;
}
return newInt;
}
问题是一个语法问题我限制以其他人编程的方式工作。我不明白如何将 CHAR“数据”存储到 INT 中。如果“数据”是一个数组不是更有意义吗?由于它接收24个整数信息存储到一个BYTE中。
double BinaryFile::from24bitToDouble(char *data) {
int32_t iValue;
// ****************************
// Start code implementation
// Task: Fill iValue with the 24bit integer located at data.
// The first byte is the LSB.
// ****************************
//iValue +=
// ****************************
// End code implementation
// ****************************
return static_cast<double>(iValue) / FACTOR;
}
bool BinaryFile::readNext(DataRecord &record)
{
const size_t RECORD_SIZE = 6;
char buffer[RECORD_SIZE];
m_ifs.read(buffer,RECORD_SIZE);
if (m_ifs) {
record.latitude = toDegrees(from24bitToDouble(&buffer[0]));
record.longitude = toDegrees(from24bitToDouble(&buffer[3]));
return true;
}
return false;
}
double BinaryFile::toDegrees(double radians) const
{
static const double PI = 3.1415926535897932384626433832795;
return radians * 180.0 / PI;
}
感谢任何帮助或提示,即使您不理解任何线索或提示也会对我有很大帮助。我只需要和某人谈谈。
【问题讨论】: