Search code examples
javajakarta-ee

Converting mac address to integer resulting in number format exception


System.out.println(Integer.parseInt("4B5CE3D77A73",16);

throws me a number format exception. and the mac address is valid one which is generated from this site http://www.miniwebtool.com/mac-address-generator/

Do i miss something here ?


Solution

  • Looks like the number is greater than what Integer can hold. Try with Long:

    Long.parseLong("4B5CE3D77A73",16)
    

    Documentation states that it throws NumberFormatException - if the string does not contain a parsable integer. Not only if the string contains invalid characters but also if the number is greater than Integer.MAX_VALUE.