Java convert string to int exception12/28/2023 Then why can we assign the number to the int variable hex? The binary representation of the numbers plays an (intended) trick on us. This number is higher than Integer.MAX_INT and therefore, cannot be represented as an int. If you convert "cafebabe" to the decimal system, you get 3,405,691,582. The reason is that the parseInt() method assumes the given number to be positive unless a minus sign precedes it. Why can't this String be converted back to an int? ![]() This attempt results in the following error: Exception in thread "main" : For input string: "cafebabe"įirst of all: The String s contains "cafebabe" as expected. ![]() Int i = Integer.parseInt(s, 16) Code language: Java ( java ) It becomes interesting (not to say: confusing) if, for example, we convert the valid int value 0xCAFEBABE into a hex String and then back into an int: int hex = 0xCAFEBABE In all the above cases, the number to be parsed must be within the range Integer.MIN_VALUE (= -2 31 = -2,147,483,648) to Integer.MAX_VALUE (= 2 31-1 = 2,147,483,647). A hexadecimal number can be parsed as follows: The parameter radix specifies the base of the number system.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |