fel*_*wcf 6 java variables hex
我已经阅读了有关的解释
int a = 0x1; //hexadecimal format
Run Code Online (Sandbox Code Playgroud)
但是,我仍然无法找到程序员应该使用0x1,0x2而不是普通整数1或2的原因...
有人可以解释一下吗?
谢谢.
人们更喜欢十六进制表示而不是十进制表示的原因有很多。计算中最常见的是位域。一些人已经提到了颜色代码,例如:
red = 0xFF0000 // 16711680 in decimal
green = 0x00FF00 // 65280 in decimal
blue = 0x0000FF // 255 in decimal
Run Code Online (Sandbox Code Playgroud)
请注意,这种颜色表示不仅比试图找出像 213545 这样的随机整数可能是什么颜色更直观,而且它占用的空间也比像(125, 255, 0)表示这样的 3 元组少(R,G,B)。十六进制表示是一种以更少的开销抽象出与 3 元组相同的想法的简单方法。
请记住,位域有很多应用,请考虑spacetime位域:
Represents x coordinate
| Represents y coordinate
| | Represents z coordinate
| | | Represents t
| | | |
1A 2B 3C 4D
Run Code Online (Sandbox Code Playgroud)
有人可能使用十六进制值的另一个原因是,有时将二进制数字记住(和表示)为两个符号比三个符号更容易。考虑x86 指令参考。我知道我的头顶0xC3是ret;我觉得更容易记住十六进制数00-FF,而不是小数0-255(我看着它,并ret最终成为195),但您的里程可能会有所不同。例如,这是我一直在从事的项目中的一些代码:
public class x64OpcodeMapping {
public static final Object[][] map = new Object[][] {
{ "ret", 0xC3 },
{ "iret", 0xCF },
{ "iretd", 0xCF },
{ "iretq", 0xCF },
{ "nop" , 0x90 },
{ "inc" , 0xFF },
};
}
Run Code Online (Sandbox Code Playgroud)
在这里使用十六进制表示法有明显的好处(更不用说一致性了)。最后,正如 Obicere 所提到的,十六进制代码通常用作错误代码。有时,它们以类似字段的方式分组。例如:
0x0X = fatal errors
0x1X = user errors
0x2X = transaction errors
// ...
// X is a wildcard
Run Code Online (Sandbox Code Playgroud)
在这样的架构下,最小的错误列表看起来像:
0x00 = reserved
0x01 = hash mismatch
0x02 = broken pipe
0x10 = user not found
0x11 = user password invalid
0x20 = payment method invalid
// ...
Run Code Online (Sandbox Code Playgroud)
请注意,这也允许我们在需要时添加新的错误0x0X。这个答案最终比我预期的要长得多,但希望我能有所启发。