增加一个十六进制值 (JAVA)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/516593/
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
Increment a Hex value (JAVA)
提问by
can you increment a hex value in Java? i.e. "hex value" = "hex value"++
你能在 Java 中增加一个十六进制值吗?即“十六进制值”=“十六进制值”++
回答by Joachim Sauer
What do you mean with "hex value"? In what data type is your value stored?
“十六进制值”是什么意思?您的值存储在哪种数据类型中?
Note that int/short/char/... don't care how your value is represented initially:
请注意, int/short/char/... 不关心您的值最初是如何表示的:
int i1 = 0x10;
int i2 = 16;
i1
and i2
will have the exact same content. Java (and most other languages as well) don't care about the notation of your constants/values.
i1
并且i2
将有完全相同的内容。Java(以及大多数其他语言)不关心常量/值的表示法。
回答by Michael Myers
Yes. All ints are binary anyway, so it doesn't matter how you declare them.
是的。无论如何,所有整数都是二进制的,因此您如何声明它们并不重要。
int hex = 0xff;
hex++; // hex is now 0x100, or 256
int hex2 = 255;
hex2++; // hex2 is now 256, or 0x100
回答by Philip Reynolds
It depends how the hex value is stored. If you've got the hex value in a string, convert it to an Integer, increment and convert it back.
这取决于十六进制值的存储方式。如果您在字符串中有十六进制值,请将其转换为整数,递增并将其转换回来。
int value = Integer.parseInt(hex, 16);
value++;
String incHex = Integer.toHexString(value);
回答by Martin Cowie
Short answer: yes. It's
简短的回答:是的。它是
myHexValue++;
Longer answer: It's likely your 'hex value' is stored as an integer. The business of converting it into a hexadecimal (as opposed to the usual decimal) string is done with
更长的答案:很可能您的“十六进制值”存储为整数。将其转换为十六进制(而不是通常的十进制)字符串的业务是用
Integer.toHexString( myHexValue )
and from a hex string with
并从一个十六进制字符串
Integer.parseInt( someHexString, 16 );
M.
M。
回答by Steve Kuo
The base of the number is purely a UI issue. Internally an integer is stored as binary. Only when you convert it to human representation do you choose a numeric base. So you're question really boils down to "how to increment an integer?".
数字的基数纯粹是 UI 问题。在内部,整数存储为二进制。只有当您将其转换为人类表示时,您才能选择数字基础。所以你的问题真的归结为“如何增加一个整数?”。