Translation of lines into the 10th. Java



  • There are lines:

    0x16
    012
    0b10
    62
    

    We need to translate them into:

    22
    10
    2
    62 
    

    respectively, but when used Integer.parseInt(String, int) Received NumberFormatExceptionHow do we solve this problem?



  • For example, such a function could be written

    private static int StringToInteger(String input)
        {
            if(input.startsWith("0x"))
            {   
                return Integer.parseInt(input.substring(2), 16);
            }
            else if(input.startsWith("0b"))
            {
                return Integer.parseInt(input.substring(2), 2);
            }
            else if(input.startsWith("0") && input.length() > 1)
            {
                return Integer.parseInt(input.substring(1), 8);
            }
            else
            {
                return Integer.parseInt(input);
            }
        }
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2