Convert string to integer hex value "Strange" behavior

I noticed that java will not allow me to store large numbers such as 2,000,000,000, i.e. 2 billion, obviously, of an integer type, but if I have the int largeHex = 0x77359400corresponding hexadecimal value, that is int largeHex = 0x77359400; it is perfectly,

Thus, my program will need to increase to 2 ^ 32, a little over 4.2 billion, I checked the hex key 0xffffffffand it allows me to store as an int type in this form,

My problem is that I have to pull the HEX line 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 have to do is have this value in an integer variable like

int largeHex = 0xffffffff

which works great?

, .

+3
6

, , , :

  • , ffffffff . Integer.parseInt(""+Long.MAX_VALUE);, . .
  • int i = 0xffffffff; i -1.
  • longs ints, , long l = 0xffffffff; l -1, 0xffffffff int. long l = 0xffffffffL;.
+2
String hex = "FFFFFFFF"; // or whatever... maximum 8 hex-digits
int i = (int) Long.parseLong(hex, 16);

int...

+6

:


System.out.println(Long.valueOf("ffffffff", 16).longValue());

:

4294967295
+2

int, , 2 ^ 31, , . , long, 64 2 ^ 63.

, =)

+2

int test = 0xFFFFFF; int test2 = Integer.parseInt(Integer.toHexString(test), 16);

^ --- ... :

int test = 0xFFFFFFFF; int test2 = Integer.parseInt(Integer.toHexString(test), 16);

^ - .

int test = 0xFFFFFFFF; int test2 = (int) Long.parseLong(Integer.toHexString(test), 16);

^ - This works great.

0
source

Source: https://habr.com/ru/post/1689753/


All Articles