在C ++中将24位整数(2s补码)转换为32位整数 [英] Converting 24 bit integer (2s complement) to 32 bit integer in C++
问题描述
dataFile.bin是具有6个字节记录的二进制文件.前3个 每条记录的字节包含纬度,而后3个字节包含 经度.每个24位值表示弧度乘以 0X1FFFFF
The dataFile.bin is a binary file with 6-byte records. The first 3 bytes of each record contain the latitude and the last 3 bytes contain the longitude. Each 24 bit value represents radians multiplied by 0X1FFFFF
这是我一直在努力的任务.我已经好几年没有做C ++了,所以它花费我的时间比我认为的要长-_-.谷歌搜索后,我看到了对我来说有意义的算法.
This is a task I've been working on. I havent done C++ in years so its taking me way longer than I thought it would -_-. After googling around I saw this algorthim which made sense to me.
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中.
The problem is a syntax issue I am restricting to working by the way the other guy had programmed this. I am not understanding how I can store the CHAR "data" into an INT. Wouldn't it make more sense if "data" was an Array? Since its receiving 24 integers of information stored into a 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;
}
我非常感谢您的帮助或提示,即使您不了解某个提示或提示也会对我有很大帮助.我只需要和某人交谈.
I appreciate any help or hints even if you dont understand a clue or hint will help me alot. I just need to talk to someone.
推荐答案
我不了解如何将CHAR数据"存储到INT中.
I am not understanding how I can store the CHAR "data" into an INT.
由于char
是数字类型,因此将它们组合成单个int
毫无问题.
Since char
is a numeric type, there is no problem combining them into a single int
.
由于它接收到存储在BYTE中的24个整数信息
Since its receiving 24 integers of information stored into a BYTE
它是24位而不是字节,因此仅需要组合三个整数值.
It's 24 bits, not bytes, so there are only three integer values that need to be combined.
在不使用条件的情况下产生相同结果的更简单方法如下:
An easier way of producing the same result without using conditionals is as follows:
int interpret24bitAsInt32(byte[] byteArray) {
return (
(byteArray[0] << 24)
| (byteArray[1] << 16)
| (byteArray[2] << 8)
) >> 8;
}
这个想法是将作为输入提供的三个字节存储到四字节int
的 upper 中的三个字节中,然后将其向下移位一个字节.这样,程序将自动对您的号码进行符号扩展,从而避免了有条件的执行.
The idea is to store the three bytes supplied as an input into the upper three bytes of the four-byte int
, and then shift it down by one byte. This way the program would sign-extend your number automatically, avoiding conditional execution.
注意:该代码不可移植,因为它假定32位整数大小.要使其可移植,请使用 <cstdint>
类型:
Note: This code is not portable, because it assumes 32-bit integer size. To make it portable use <cstdint>
types:
int32_t interpret24bitAsInt32(int8_t[] byteArray) {
return (
(byteArray[0] << 24)
| (byteArray[1] << 16)
| (byteArray[2] << 8)
) >> 8;
}
还假定24位数字的最高有效字节存储在byteArray
的初始元素中,然后是中间元素,最后是最低有效字节.
It also assumes that the most significant byte of the 24-bit number is stored in the initial element of byteArray
, then comes the middle element, and finally the least significant byte.
这篇关于在C ++中将24位整数(2s补码)转换为32位整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!