java 如何使用java在短变量中存储十六进制值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16850656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:09:46  来源:igfitidea点击:

How to store hexadecimal value in short variable using java?

java

提问by Sudhakar

When I assigned vlanid=0x960.(hexadecimal value) directly,It works very well. But when I use java function for same purpose,It get stored as decimal value. I attached the code here.

当我vlanid=0x960直接赋值.(十六进制值)时,效果很好。但是当我出于同样的目的使用 java 函数时,它被存储为十进制值。我在这里附上了代码。

int vlanid,

short vlanid1;

vlanid= (int) jSpinner1.getModel().getValue();

vlanid1 = Short.parseShort(((Integer.toHexString(vlanid))),16);

回答by Craig

It appears that you were not understanding the information given in the comments, so here is an example which shows you that it doesn't matter if it was initially stored as a "decimal" or "hexadecimal"

看来您没有理解评论中给出的信息,因此这里有一个示例,它向您表明最初存储为“十进制”还是“十六进制”并不重要

    short short1 = 0x10;
    short short2 = 16;
    short short3 = (short) (short1 + short2);
    System.out.println("Short1: " + short1 + ", " + Integer.toHexString(short1));
    System.out.println("Short2: " + short2 + ", " + Integer.toHexString(short2));
    System.out.println("Short3: " + short3 + ", " + Integer.toHexString(short3));

Output:

输出:

Short1: 16, 10
Short2: 16, 10
Short3: 32, 20

EDIT:

编辑:

In response to your comment I have added an example of going from an intto a shortusing two different methods. What you seem to be getting caught on is that the int should be in hexadecimal value; this is not how values are stored (doesn't matter whether it is int, short, long, etc.). Allof these types are stored internally as two's complement integers(See this post). Thus in the example below both int1and int2represent the same value

为了回应您的评论,我添加了int一个short使用两种不同方法从 an到 a的示例。你似乎被抓住的是 int should be in hexadecimal value; 这不是值的存储方式(不管它是 int、short、long 等)。所有这些类型都在内部存储为二进制补码整数参见这篇文章)。因此在下面的示例中,int1int2代表相同的值

    int int1 = 0x01b213d4;  // Stored as: 1101100100001001111010100
    int int2 = 28447700;    // Stored as: 1101100100001001111010100

    short short1Cast = (short) int1;
    short short1Mask = (short) (int1 & 0xFFFF);
    short short2Cast = (short) int2;
    short short2Mask = (short) (int2 & 0xFFFF);

    System.out.println("Int1: " + int1 + ", " + Integer.toHexString(int1) + ", " + short1Cast + ", " + short1Mask);     
    System.out.println("Int2: " + int2 + ", " + Integer.toHexString(int2) + ", " + short2Cast + ", " + short2Mask);

Output:

输出:

Int1: 28447700, 1b213d4 | Short1: 5076, 5076
Int2: 28447700, 1b213d4 | Short2: 5076, 5076

I have kept both the cast method and mask method in here because you explicitly made reference to masking the integer value. Unless you have to do something special with bit masking, I would highly suggest using the easier cast approach as there is no functional difference in the output value.

我在这里保留了 cast 方法和 mask 方法,因为您明确引用了屏蔽整数值。除非您必须对位屏蔽做一些特殊的事情,否则我强烈建议使用更简单的转换方法,因为输出值没有功能差异。

回答by Peter Lawrey

The number is always stored as two-complement binary. However when you display this number you generally see the result of Short.toString(short) which produces a decimal by default.

该数字始终存储为二进制补码。然而,当你显示这个数字时,你通常会看到 Short.toString(short) 的结果,默认情况下它会产生一个小数。

The format for how you describe the number is not important and not stored. Only the value matters and this can be display as decimal (the default) or any other base you want from base 2 up to base 36.

您如何描述数字的格式并不重要,也不会存储。只有值很重要,这可以显示为十进制(默认值)或任何其他您想要的从基数 2 到基数 36 的基数。