I have noticed java will not allow me to store large numbers such as 2000000000, i.e 2 billion obviously to an integer type, but if i store the corresponding hex value i.e int largeHex = 0x77359400
; this is fine,
我注意到java不会允许我存储大量的数据,比如2000000000,I。e 20亿显然是一个整数类型,但如果我存储相应的十六进制值i。e int largeHex = 0x77359400;这是好,
So my program is going to need to increment up2 2^32, just over 4.2 billion, I tested out the hex key 0xffffffff
and it allows me to store as type int in this form,
所以我的计划是需要增加呼吁2 ^ 32岁,刚刚超过42亿,我测试了六角扳手0 xffffffff,它允许我int类型存储在这种形式,
my problem is i have to pull a HEX string from the program.
我的问题是我必须从程序中取出一个十六进制字符串。
Example
例子
sT = "ffffffff";
int hexSt = Integer.valueOf(sT, 16).intValue();
this only works for smaller integer values
这只适用于较小的整数值
I get an error
我得到一个错误
Exception in thread "main" java.lang.NumberFormatException: For input string: "ffffffff"
All i need to do is have this value in an integer variable such as
我需要做的就是将这个值放在一个整数变量中,比如
int largeHex = 0xffffffff
which works fine?
工作好吗?
Im using integers because my program will need to generate many values
我使用整数,因为我的程序需要生成许多值
6 个解决方案
#1
2
Well, it seems there is nothing to add to the answers, but it's worth it to clarify:
好吧,似乎没有什么可以增加答案,但值得澄清一下:
- It throws an exception on parsing, because
ffffffff
is too big for an integer. ConsiderInteger.parseInt(""+Long.MAX_VALUE);
, without using hex representation. The same exception is thrown here. - 它在解析时抛出一个异常,因为ffffff对于整数来说太大了。考虑int . parseint(“”+Long.MAX_VALUE);这里抛出了相同的异常。
-
int i = 0xffffffff;
setsi
to-1
. - int i = 0 xffffffff;我设置为1。
- If you already decided to use longs instead of ints, note that
long l = 0xffffffff;
will setl
to-1
as well, since0xffffffff
is treated as an int. The correct form islong l = 0xffffffffL;
. - 如果您已经决定使用longs而不是ints,请注意long l = 0xffffffff;将l设置为-1,因为0xffffffff被视为int,正确的形式是long l = 0xffffffffffffffffffl;
#2
6
String hex = "FFFFFFFF"; // or whatever... maximum 8 hex-digits
int i = (int) Long.parseLong(hex, 16);
Gives you the result as a signed int ...
将结果作为一个签名int…
#3
2
How about using:
如何使用:
System.out.println(Long.valueOf("ffffffff", 16).longValue());
Which outputs:
输出:
4294967295
#4
2
The int
data type, being signed will store values up to about 2^31, only half of what you need. However, you can use long
which being 64 bits long will store values up to about 2^63.
签署的int数据类型,将存储值约2 ^ 31日只有一半的你所需要的东西。不过,您可以使用长是64位将存储值约2 ^ 63。
Hopefully this will circumvent your entire issue with hex values =)
希望这能绕过十六进制值=的问题)
#5
0
Consider using BigInteger
考虑使用BigInteger
http://download.oracle.com/javase/6/docs/api/java/math/BigInteger.html
http://download.oracle.com/javase/6/docs/api/java/math/BigInteger.html
#6
0
int test = 0xFFFFFF; int test2 = Integer.parseInt(Integer.toHexString(test), 16);
智力测试= 0 xffffff;int test2 = Integer.parseInt(Integer.toHexString(test), 16);
^---That works as expected... but:
^————工作如预期……但是:
int test = 0xFFFFFFFF; int test2 = Integer.parseInt(Integer.toHexString(test), 16);
智力测试= 0 xffffffff;int test2 = Integer.parseInt(Integer.toHexString(test), 16);
^--That throws a number format exception.
^——抛出一个数字格式异常。
int test = 0xFFFFFFFF; int test2 = (int)Long.parseLong(Integer.toHexString(test), 16);
智力测试= 0 xffffffff;int test2 = (int)Long.parseLong(Integer.toHexString(test), 16);
^--That works fine.
^——没问题。
#1
2
Well, it seems there is nothing to add to the answers, but it's worth it to clarify:
好吧,似乎没有什么可以增加答案,但值得澄清一下:
- It throws an exception on parsing, because
ffffffff
is too big for an integer. ConsiderInteger.parseInt(""+Long.MAX_VALUE);
, without using hex representation. The same exception is thrown here. - 它在解析时抛出一个异常,因为ffffff对于整数来说太大了。考虑int . parseint(“”+Long.MAX_VALUE);这里抛出了相同的异常。
-
int i = 0xffffffff;
setsi
to-1
. - int i = 0 xffffffff;我设置为1。
- If you already decided to use longs instead of ints, note that
long l = 0xffffffff;
will setl
to-1
as well, since0xffffffff
is treated as an int. The correct form islong l = 0xffffffffL;
. - 如果您已经决定使用longs而不是ints,请注意long l = 0xffffffff;将l设置为-1,因为0xffffffff被视为int,正确的形式是long l = 0xffffffffffffffffffl;
#2
6
String hex = "FFFFFFFF"; // or whatever... maximum 8 hex-digits
int i = (int) Long.parseLong(hex, 16);
Gives you the result as a signed int ...
将结果作为一个签名int…
#3
2
How about using:
如何使用:
System.out.println(Long.valueOf("ffffffff", 16).longValue());
Which outputs:
输出:
4294967295
#4
2
The int
data type, being signed will store values up to about 2^31, only half of what you need. However, you can use long
which being 64 bits long will store values up to about 2^63.
签署的int数据类型,将存储值约2 ^ 31日只有一半的你所需要的东西。不过,您可以使用长是64位将存储值约2 ^ 63。
Hopefully this will circumvent your entire issue with hex values =)
希望这能绕过十六进制值=的问题)
#5
0
Consider using BigInteger
考虑使用BigInteger
http://download.oracle.com/javase/6/docs/api/java/math/BigInteger.html
http://download.oracle.com/javase/6/docs/api/java/math/BigInteger.html
#6
0
int test = 0xFFFFFF; int test2 = Integer.parseInt(Integer.toHexString(test), 16);
智力测试= 0 xffffff;int test2 = Integer.parseInt(Integer.toHexString(test), 16);
^---That works as expected... but:
^————工作如预期……但是:
int test = 0xFFFFFFFF; int test2 = Integer.parseInt(Integer.toHexString(test), 16);
智力测试= 0 xffffffff;int test2 = Integer.parseInt(Integer.toHexString(test), 16);
^--That throws a number format exception.
^——抛出一个数字格式异常。
int test = 0xFFFFFFFF; int test2 = (int)Long.parseLong(Integer.toHexString(test), 16);
智力测试= 0 xffffffff;int test2 = (int)Long.parseLong(Integer.toHexString(test), 16);
^--That works fine.
^——没问题。