Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Convert Integer to Hex String in C++



Hexadecimal is a base 16 numbering system that uses 16 digits (from 0 to 9, and then A to F) to represent numbers. It is commonly used in computing systems to represent and store data. In this article, we will learn how to convert a normal integer to a hexadecimal string using C++ program.

Integer to Hex String Conversion

Here is a list of approaches for converting an integer to a hexadecimal, which we will be discussing in this article with stepwise explanation and complete example codes.

Using std::stringstream

The stringstream is a class in C++ <sstream> library, which is used to treat a string as a stream of characters. Using this class, we can read and write data directly into a string instead of a console. Let's see how to use it.

// Create a stringstream object
stringstream ss;

// This sets the stream to hexadecimal format 
// and inserts the number 255 into the stream.
ss << hex << 255;

When 255 is inserted into the stream, it is converted to hexadecimal format and printed as FF. Now, let's ook an example code to convert an integer to a hexadecimal string.

Example

In the code below, we create a stringstream object and insert the integer 255 into it. Then, we use the str() function to get the string representation of the stream. Finally, we print the string.

#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

int main() {
    int num = 255;
    stringstream ss;
    ss << hex << num;  // Convert to hexadecimal
    string hexStr = ss.str();
    cout << hexStr << endl; // Output: "ff"
}

The output of the above code will be:

ff

Using std::format

The std::format function is a member function of the format library in C++. It is used to format data into a string. Let's see an example of how to use it.

Example

In the code below, we use the format function to convert the integer 255 to a hexadecimal string. Then, we print the string.

#include <iostream>
#include <iostream>
#include <format> 
using namespace std;

int main() {
    int num = 255;
    string hexStr = format("0x{:x}", num); 
    cout << hexStr << endl; 
}

The output of the above code will be:

0xff

Note: The above code will only compile, if you're using Microsoft Visual Studio 2022 or later, with /std:c++20.

Using std::printf

The std::printf function is a member function of the cstdio library in C++. It is print formatted data to the console. Let's see an example of how to use it.

Example

In the code below, we use the std::printf function to convert the integer 255 to a hexadecimal string. Then, we print the string.

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int num = 255;
    char hexStr[10];
    sprintf(hexStr, "0x%x", num);
    cout << hexStr << endl;
}

The output of the above code will be:

0xff

Manual Conversion

Manual conversion involves using algorithm to convert a number to a hexadecimal string. Let's see an example of how to use it.

Example

In the code below, we define a function called intToHex() that takes an integer as input and returns a hexadecimal string. The function uses a loop to convert the integer to a hexadecimal string. If the number is 0, it returns "0". Otherwise, it loops through the number and converts each digit to a hexadecimal character. Finally, it returns the string.

#include <iostream>
#include <string>
using namespace std;

string intToHex(int num) {
    if (num == 0) return "0";
    string hexChars = "0123456789ABCDEF";
    string result;
    while (num > 0) {
        result = hexChars[num % 16] + result;
        num /= 16;
    }
    return result;
}

int main() {
    int num = 255;
    cout << intToHex(num) << endl;
}

The output of the above code will be:

FF
Updated on: 2025-04-17T18:33:36+05:30

21K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements