C++

Converting an integer to hexadecimal

To convert an integer into 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.

Decimal Hex
equivalent
Binary Hex
equivalent
0 0 0000 0
1 1 0001 1
2 2 0010 2
3 3 0011 3
4 4 0100 4
5 5 0101 5
6 6 0110 6
7 7 0111 7
8 8 1000 8
9 9 1001 9
10 a 1010 a
11 b 1011 b
12 c 1100 c
13 d 1101 d
14 e 1110 e
15 f 1111 f

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

Integer Decimal number
( Integer / 16.0 )
Whole number
part
Fractional part Fractional part * 16 Hex equivalent Hex number
425 425 / 16.0 = 26.5625 26 .5625 .5625 * 16 = 9 9 9
26 26 / 16.0 = 1.625 1 .625 .625 * 16 = 10 a ( 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 number into a hexadecimal, we find the 2’s complement of the number which is a binary string and convert it to hexadecimal.

Integer Binary 2’s complement Hex number
-10 00000000 00000000 00000000 00001010 11111111 11111111 11111111 11110110 ff ff ff f6


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


Copyright (c) 2019-2021, Algotree.org.
All rights reserved.