Converting an integer to hexadecimal

To convert a positive integer into a hexadecimal, we do the below
Step a. Divide the integer by 16 and obtain the whole number part and the fractional part.
Step b. Multiply the fractional part by 16. Get the hex equivalent of the result thus obtained.
Step c. Perform steps a) & b) on the whole number part and prepend the hex equivalent thus obtained with the earlier obtained hex number.

Below table shows an example of converting a positive integer (425) to a hexadecimal string (1a9).

IntegerDecimal number
( Integer / 16.0 )
Whole number
part
Fractional partFractional part * 16Hex equivalentHex number
425425 / 16.0 = 26.562526.5625.5625 * 16 = 999
2626 / 16.0 = 1.6251.625.625 * 16 = 10a ( Hex equivalent of 10 )a9
1 ( As 1 is less than 16, we prepend 1
to the hex string generated so far )
—————1a9

For converting a negative integer into a hexadecimal, we find the 2’s complement of the number which is a binary string and convert it to hexadecimal.

IntegerBinary2’s complementHex number
-1000000000 00000000 00000000 0000101011111111 11111111 11111111 11110110ff ff ff f6


Decimal to Hex equivalent table

DecimalHex
equivalent
BinaryHex
equivalent
0000000
1100011
2200102
3300113
4401004
5501015
6601106
7701117
8810008
9910019
10a1010a
11b1011b
12c1100c
13d1101d
14e1110e
15f1111f


C++ : Program for converting an integer to a hexadecimal.

#include<iostream>
#include<string>
#include<bitset>
#include<map>

using namespace std;

string Integer_To_Hex (int num) {

    //if (num == 0)
    //    return "0";

    char hex_map[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', \
                         'a', 'b', 'c', 'd', 'e', 'f' };

    map<std::string, std::string> map_bin_to_hex { {"0000", "0"},
                                                   {"0001", "1"},
                                                   {"0010", "2"},
                                                   {"0011", "3"},
                                                   {"0100", "4"},
                                                   {"0101", "5"},
                                                   {"0110", "6"},
                                                   {"0111", "7"},
                                                   {"1000", "8"},
                                                   {"1001", "9"},
                                                   {"1010", "a"},
                                                   {"1011", "b"},
                                                   {"1100", "c"},
                                                   {"1101", "d"},
                                                   {"1110", "e"},
                                                   {"1111", "f"},
                                                 };

    string hex("");

    if (num > 0) { // For positive integer
        while (num > 15) {
            double decimal_number = num / 16.0;
            int whole_number_part = (int) decimal_number;
            double fractional_part = decimal_number - whole_number_part;
            hex = hex_map[(int) (fractional_part * 16)] + hex;
            num = whole_number_part;
        }

        hex = hex_map[num] + hex;

    } else { // For a negative integer, return hex of two's complement.
        bitset<32> bits(num);
        string bin_string = bits.to_string();
        while (bin_string.size()) {
            hex = map_bin_to_hex[bin_string.substr(bin_string.size() - 4, 4)] + hex;
            bin_string = bin_string.substr(0, bin_string.size() - 4);
        }
    }
    return hex;
}

int main() {

    int num;
    cout << "Enter integer number : ";
    cin >> num;
    cout << "Hexadecimal : " << Integer_To_Hex(num) << endl;
    return 0;
}

Output

Enter integer number : 100
Hexadecimal : 64

Enter integer number : 16
Hexadecimal : 10

Enter integer number : 26
Hexadecimal : 1a

Enter integer number : -10
Hexadecimal : fffffff6

Enter integer number : -100
Hexadecimal : ffffff9c

Enter integer number : -1
Hexadecimal : ffffffff

Enter integer number : 0
Hexadecimal : 00000000


ยฉ 2019-2026 Algotree.org | All rights reserved.

This content is provided for educational purposes. Feel free to learn, practice, and share knowledge.
For questions or contributions, visit algotree.org

"I'm not a great programmer; I'm just a good programmer with great habits. - Kent Beck"