假设我有以下内容。
int main(){
int x = 0x02;
int y = 0x72;
//Figure out how to put 0x02 and 0x72 together to make 0x272.
}
正如评论所说,我想弄清楚如何将十六进制值放在一起。这不是简单的数字相加。
上面的代码是一个更大项目的一部分,我想知道如何解决这个问题。
最佳答案
是的,这是一个简单的加法问题,因为乘法是很多简单的加法。 :-)
我建议使用乘法(并使用无符号整数):
unsigned int x = 0x02;
unsigned int y = 0x72;
unsigned int combined = (x * 256) + y;
// Or the equivalent
unsigned int result = (x * 0x100) + y;
此解决方案独立于平台,不依赖于 Endinanness。
关于c++ - 如何附加到彼此相邻的十六进制值然后显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36519281/