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!
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 July 2026
Contest runs July 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.