Convert A Number To Hexadecimal
To solve this coding challenge, you'll need to convert both positive and negative integers into their hexadecimal string representations. This must be done without using any built-in library functions dedicated to hexadecimal conversion.
Explanation
We begin by understanding how hexadecimal numbers are structured. Hexadecimal (or hex) is a base-16 number system, which means it has sixteen symbols: 0-9 to represent values zero to nine, and a-f to represent values ten to fifteen.- For positive integers:
- We repeatedly divide the number by 16 and track the remainders.
- The remainders correspond to the hexadecimal digits, with the least significant remainder becoming the rightmost digit.
- Build the hex string by converting these remainders into their hex equivalent characters and concatenating in reverse order of their discovery.
- For negative integers:
- Use the two’s complement method because negative numbers in binary use this representation.
-
For an
n
2^n
n
- Once the two’s complement is calculated, proceed as with positive numbers to convert to hex.
- Handling edge cases:
- Directly return "0" if the input number is zero.
- For negative numbers converted to 32-bit two’s complements, ensure there are no leading zeros unless the number is zero itself.
- Start with checking if the input number is zero.
- If it is zero, directly return "0".
- Handle negative numbers:
-
For negative numbers, add \(2^{32}\) (which is
4294967296
- Initialize helper variables:
-
hex_characters
-
hex_result
- Loop to construct the hexadecimal string:
- Continue to process the input number until the value becomes zero.
- In each loop iteration:
-
Calculate the current hexadecimal digit by getting the remainder of the input number modulo 16 (
input_number % 16
-
Lookup the corresponding character from
hex_characters
hex_result
-
Reduce the input number by performing integer division by 16 (
input_number // 16
- Return the constructed hex string after the loop exits.
Pseudocode:
The pseudocode below will demonstrate the detailed steps and operations performed including comments to ensure a comprehensive understanding.
# Converts an integer to its hexadecimal string representation
FUNCTION convertToHexadecimal(input_number):
# Base case: if input_number is zero, return "0"
IF input_number IS 0:
RETURN "0"
# If input_number is negative, adjust by adding 2^32 for two's complement representation
IF input_number < 0:
input_number = input_number + 2^32
# Initialize a string with hexadecimal characters
hex_characters = "0123456789abcdef"
# Initialize an empty result string
hex_result = ""
# Process the input number until it becomes zero
WHILE input_number > 0:
# Find the remainder when input_number is divided by 16
hex_digit_index = input_number % 16
# Append the corresponding hex character to the result (in reverse order)
hex_result = hex_characters[hex_digit_index] + hex_result
# Update input_number by floor division with 16
input_number = input_number // 16
# Return the final constructed hex string
RETURN hex_result