-3

When i paste parseInt("3cf1bb13f1c96", 16) to javascript console, it successfuly converts to integer value which is 1072142774901910 but when i use Integer.parseInt("3cf1bb13f1c96", 16) in java it prints something

Exception in thread "main" java.lang.NumberFormatException: For input string: "3cf1bb13f1c96"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Main.example(Main.java:line)
    at Main.example(Main.java:line)
Garbarius
  • 5
  • 2
  • 1
    Does this answer your question? [Convert hex string to int](https://stackoverflow.com/questions/11194513/convert-hex-string-to-int) – cegredev May 24 '20 at 10:27
  • 1
    This is in `Java` but why do you have a `Javascript` tag attached too? – Adnane Ar May 24 '20 at 10:27

1 Answers1

1

The value you are parsing is out of Integer range. In Java, the integer ranges from -2,147,483,648 to +2,147,483,647 . Try to use Long instead:

 Long.parseLong("3cf1bb13f1c96", 16) 

it parses successfully:

> Task :Foo.main()
1072142774901910
GiorgosDev
  • 1,757
  • 1
  • 14
  • 16
  • @Garbarius - Do not forget to accept the answer so that future visitors can also use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 24 '20 at 10:36
  • @ArvindKumarAvinash I already know it but i guess the one who need to learn how to do it is you because 10 minutes should pass before you can accept a answer – Garbarius May 24 '20 at 10:41
  • Oh...is it so? Thanks, @Garbarius for this information. I really didn't know it. Wish you success! – Arvind Kumar Avinash May 24 '20 at 10:45