在Java中为int动态添加0x前缀
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20984364/
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
Dynamically add 0x prefix to int in Java
提问by jesscarter1
int i = 16777215;
void draw(int color) {
}
Lets say void draw expects the color to start with the 0x prefix like so
假设 void draw 期望颜色以 0x 前缀开头,就像这样
draw(0x16777215)
If I try this it throws a syntax error
如果我尝试这个它会抛出一个语法错误
draw(0x + i)
Invalid hex literal number
int i cannot contain the 0x part, it needs to be dynamically added
int i 不能包含 0x 部分,需要动态添加
thanks
谢谢
回答by rgettman
Just pass i
. There is no such thing as a method that expects a number that starts with 0x
. If it takes an int
, then it will take any int
, specified as a hexadecimal literal with 0x
or not.
只要通过i
。不存在期望以 开头的数字的方法0x
。如果它采用int
,那么它将采用 any int
,指定为带有0x
或不带有的十六进制文字。
Also, be careful. 0x16777215
is a different number than 16777215
.
另外,要小心。 0x16777215
是一个不同的数字16777215
。
回答by yshavit
0x1234
is just another way of writing an integer value. For instance, 0xff
and 255
are exactly the same thing. draw(0x16777215)
and draw(376926741)
are identical calls. At the bytecode level, there's not anydifference. The only difference is to how it appears to a human reading the code; sometimes it's easier to think in terms of bytes, which are easily expressed in hex.
0x1234
只是写整数值的另一种方式。例如,0xff
和255
是完全一样的东西。draw(0x16777215)
并且draw(376926741)
是相同的调用。在字节码级别,没有任何区别。唯一的区别是人类阅读代码的方式;有时用字节来考虑更容易,字节很容易用十六进制表示。
If a number starts with 0x
, it means the rest of the digits are interpreted as hex. Instead of 0x16777215
, I'm guessing you wanted 16777215
(without the 0x
), which is also 0xFFFFFF
.
如果数字以 开头0x
,则表示其余数字被解释为十六进制。而不是0x16777215
,我猜你想要16777215
(没有0x
),这也是0xFFFFFF
.
The Oracle tutorial on primitive datatypeshas a bit more information (and more authority than I do :) ).
关于原始数据类型的Oracle 教程有更多的信息(而且比我做的更权威:))。
To demonstrate that it's all the same under the hood, you can look at some disassembled code. Consider this class:
为了证明它的内幕完全相同,您可以查看一些反汇编代码。考虑这个类:
public class Hexes {
public static int alpha = 0x16777215;
public static int beta = 376926741;
}
Running javap -c Hexes
after compiling Hexes.java
will show you what's happening under the hood:
javap -c Hexes
编译后运行Hexes.java
将向您展示幕后发生的事情:
Compiled from "Hexes.java"
public class Hexes {
public static int alpha;
public static int beta;
public Hexes();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
static {};
Code:
0: ldc #2 // int 376926741
2: putstatic #3 // Field alpha:I
5: ldc #2 // int 376926741
7: putstatic #4 // Field beta:I
10: return
}
The block labeled static {}
is the class' static initializer, where alpha
and beta
are set. Note that the two fields (alpha and beta) are both assigned the same int value. If you were to write a short program to print the values out, you'd see the same value for each:
标记的块static {}
是类静态初始化,其中alpha
和beta
被设置。请注意,两个字段(alpha 和 beta)都分配了相同的 int 值。如果您要编写一个简短的程序来打印这些值,您会看到每个值都相同:
public class HexesPrinter {
public static void main(String[] args) {
System.out.println(Hexes.alpha);
System.out.println(Hexes.beta);
}
}
Output:
输出:
376926741
376926741
回答by Abesalomi Gogatishvili
I guess you need this:
我想你需要这个:
Integer.valueOf(String.valueOf(i), 16)
回答by Fuhrmanator
You need to understand first the difference between specifying base 10 numbers and base 16 numbers (hex) in Java. As others stated 0x
is a prefix to a number that says it's in base 16 (hex).
您首先需要了解在 Java 中指定基数为 10 的数字和基数为 16 的数字(十六进制)之间的区别。正如其他人所说,它0x
是一个数字的前缀,表示它以 16 进制(十六进制)表示。
It's useful to specify colors in hex notation because they have components (Alpha, R, G, B), which are not obvious when you put them in base 10. See this answer for more details: https://stackoverflow.com/a/8633244/1168342
以十六进制表示法指定颜色很有用,因为它们具有分量(Alpha、R、G、B),当您将它们放在基数 10 中时,这些分量并不明显。有关更多详细信息,请参阅此答案:https: //stackoverflow.com/a /8633244/1168342