1. 取绝对值
int value = -123;
qint16 absValue = qAbs(value );
qDebug() << "Absolute Value: " << absValue;
2. 将绝对值转换为二进制
std::bitset<16> binary(absValue);
qDebug() << "Binary Representation: " << QString::fromStdString(binary.to_string());
3. 按位取反
quint16 invertedValue = ~absValue;
qDebug() << "Inverted Value (in binary): " << QString::number(invertedValue, 2);
4. 加1 (补码)
qint16 result = invertedValue + 1;
qDebug() << "After adding 1 (in binary): " << QString::number(result, 2);
5. 转换为十六进制
QString hexValue = QString::number(result, 16).toUpper();
qDebug() << "Hexadecimal Result: " << hexValue;
6.取后两个字节,就是负数的十六进制,使用的话,把这个十六进制转成十进制的数,减去65536,就是对应的负数
reshex = hexValue.rightJustified(4, '0').right(4);
qDebug() << "Right two bytes: " << reshex;
bool ok;
int resValue = reshex.toInt(&ok, 16);
qDebug() << "result number: " << resValue;