
Problem
Solution
Submissions
Number to Hexadecimal
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 10
Write a C# program to convert a given integer to its hexadecimal representation without using built-in methods like ToString("X"). For negative integers, use the two's complement method and output the result as a 32-bit representation.
Example 1
- Input: num = 26
- Output: "1a"
- Explanation:
- 26 in decimal equals 1a in hexadecimal.
Example 2
- Input: num = -1
- Output: "ffffffff"
- Explanation:
- -1 in two's complement 32-bit representation is all 1's, which in hexadecimal is "ffffffff".
Constraints
- -2^31 ≤ num ≤ 2^31 - 1
- Time Complexity: O(log n), where n is the input number
- Space Complexity: O(1) not counting the output string
- You cannot use built-in methods to directly convert the number to hexadecimal
Editorial
My Submissions
All Solutions
| Lang | Status | Date | Code |
|---|---|---|---|
| You do not have any submissions for this problem. | |||
| User | Lang | Status | Date | Code |
|---|---|---|---|---|
| No submissions found. | ||||
Solution Hints
- Use bitwise operations to extract 4 bits at a time.
- Remember that hexadecimal uses characters 0-9 and a-f.
- For negative numbers, use the two's complement representation.
- Build the result string from right to left, then reverse it.