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.
  1. 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.
  2. For negative integers:
    • Use the two’s complement method because negative numbers in binary use this representation.
    • For an
      n
      bit integer, the two's complement is found by adding
      2^n
      to the number. Here
      n
      is 32 since we are dealing with 32-bit signed integers (-2^31 to 2^31-1).
    • Once the two’s complement is calculated, proceed as with positive numbers to convert to hex.
  3. 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.

    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
    
                                            

    Detailed Steps in Pseudocode:

  4. Start with checking if the input number is zero.
    • If it is zero, directly return "0".
  5. Handle negative numbers:
    • For negative numbers, add \(2^{32}\) (which is
      4294967296
      ) to transform the number into a positive equivalent via two’s complement method.
  6. Initialize helper variables:
    • hex_characters
      is a string containing '0123456789abcdef', which will help in mapping remainders to their hex equivalents.
    • hex_result
      is initialized as an empty string, which will store the accumulated hexadecimal result.
  7. 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
        and prepend it to
        hex_result
        .
      • Reduce the input number by performing integer division by 16 (
        input_number // 16
        ).
  8. Return the constructed hex string after the loop exits.
By following these steps and understanding the intricacy of binary and hexadecimal representations, you can tackle converting a number to its hex representation without relying on built-in functions.