如何附加到相邻的十六进制值,然后显示
How to attach to hex values next to each other and then display
假设我有以下内容。
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;
这个解决方案是独立于平台的,不依赖于Endinaness。
类似:
int main(){
int x = 0x02;
int y = 0x72;
int z = x << 8 | y;
printf("0x%xn", z);
z = y << 8 | x;
printf("0x%xn", z);
}
输出:
0x272
0x7202
相关文章:
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++
- Gtkmm - 关闭窗口然后显示另一个窗口的正确方法
- 如何存储多个用户输入,然后以C++显示它们
- 如何从文本文件中获取值,然后用它们执行一些计算(例如乘以并显示它们)?
- 将25个甚至整数存储到一个名为intlist的整数阵列中,然后在屏幕上显示数组,并在屏幕上显示名为fivintegers
- 根据标准对结构向量进行排序,然后显示结果
- 显示然后删除链接列表C
- 我如何调用我的3个数据成员中每个人的设置方法,然后显示由我的设置方法设置的值
- 如何增加几个月循环.然后显示
- 如何使用我的 if-else 语句根据用户权重的输入执行特定操作,然后将其显示到控制台?
- c++将var从一个函数传递到另一个函数,然后在主函数中显示结果
- 对输入'age'名称进行排序,然后显示结果
- 如何附加到相邻的十六进制值,然后显示
- 尝试在单击按钮时捕获编辑框中的文本,然后显示到另一个编辑框
- 如何更改数组的值然后显示整个新数组
- 在C++中,我需要创建一个程序,该程序在输入某个数字时循环并停止,然后显示最大值和最小值
- Qt:隐藏然后显示QQuickView防止未来的鼠标事件
- 我如何创建一个c++代码,允许用户输入几个百分比分数,然后显示相应的字母分数
- 仅使用素数 2、3 和 5 生成序列,然后显示第 n 项 (C++)