Given a decimal integer and a target base (2 through 16), convert the number to its string representation in the target base. Use uppercase letters A through F for digits 10 through 15 in bases greater than 10. Preserve the negative sign for negative numbers. Return null for null input or invalid base values outside the range 2 to 16.

public static String convertToBase(Integer inputNumber, Integer toBase)

Examples:

Input: inputNumber = 10, toBase = 2 Output: '1010' Explanation: 10 in binary is 1010 Input: inputNumber = 255, toBase = 16 Output: 'FF' Explanation: 255 in hexadecimal is FF Input: inputNumber = 0, toBase = 8 Output: '0' Explanation: Zero is zero in any base Input: inputNumber = -42, toBase = 2 Output: '-101010' Explanation: Negative sign is preserved, 42 in binary is 101010
Apex Code Editor

Welcome to Lightning Challenge!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay Mar/April 2026

Contest runs March 3 - April 30. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.