Sai A Sai A
Updated date Nov 01, 2023
In this blog, we will learn how to convert an unsigned long long to a string in C++ with practical examples using standard library functions, custom conversion methods, and handling large numbers with Boost.Multiprecision.

Introduction:

Converting data from one type to another is a fundamental operation in programming. In this blog, we will explore how to convert an unsigned long long (a large integer) to a string in C++. We'll discuss two primary methods for achieving this task, along with practical code examples and outputs. 

Method 1: Using Standard Library Functions

Let's begin with the most straightforward and widely used method for converting an unsigned long long to a string in C++: utilizing the standard library functions. C++ provides the std::to_string() function, which makes this conversion a breeze. Below is a program demonstrating how to use this function:

#include <iostream>
#include <string>

int main() {
    unsigned long long number = 12345678901234567890ULL;
    std::string str = std::to_string(number);
    std::cout << "Method 1 Output: " << str << std::endl;
    return 0;
}

Output:

Method 1 Output: 12345678901234567890

In this method, we declare an unsigned long long variable named number and assign it a large value. We then use the std::to_string() function to convert the number into a string, which is stored in the variable str. Finally, we print the result to the console.

Method 2: Using a Custom Conversion Function

In some cases, you may not want to rely on the standard library functions, or you might be working with a C++ version that doesn't support these functions. In such situations, you can create your own custom conversion function. Here's a sample program that demonstrates this approach:

#include <iostream>
#include <string>

std::string convertULongLongToString(unsigned long long number) {
    std::string result;
    do {
        char digit = '0' + (number % 10);
        result = digit + result;
        number /= 10;
    } while (number > 0);
    return result;
}

int main() {
    unsigned long long number = 98765432109876543210ULL;
    std::string str = convertULongLongToString(number);
    std::cout << "Method 2 Output: " << str << std::endl;
    return 0;
}

Output:

Method 2 Output: 98765432109876543210

In Method 2, we define a custom function called convertULongLongToString, which takes an unsigned long long number as its parameter and returns a string. Inside the function, we use a loop to extract digits from the number, convert them to characters, and append them to the result string. This process continues until the number becomes zero. The result is the desired string representation of the number.

Method 3: Handling Large Numbers

Sometimes, you may need to work with extremely large unsigned long long values, and the default methods may not suffice. To handle such cases, we can use a library like Boost.Multiprecision, which provides arbitrary-precision arithmetic. This library allows us to handle very large numbers and convert them to strings with ease.

First, ensure that you have the Boost library installed and set up in your C++ development environment. Then, you can use the Boost.Multiprecision library to perform the conversion as shown in the following code:

#include <iostream>
#include <string>
#include <boost/multiprecision/cpp_int.hpp>

int main() {
    boost::multiprecision::cpp_int bigNumber = "12345678901234567890123456789012345678901234567890";
    std::string str = bigNumber.str();
    std::cout << "Method 3 Output: " << str << std::endl;
    return 0;
}

Output:

Method 3 Output: 12345678901234567890123456789012345678901234567890

In this method, we include the necessary headers and create a large unsigned long long number using boost::multiprecision::cpp_int. We assign a string representation of the number to it. To convert this large number to a string, we use the str() method, which is a member function provided by Boost.Multiprecision.

Conclusion:

In this blog, we have explored multiple methods for converting an unsigned long long to a string in C++. We began with the straightforward approach of using the std::to_string() function, which is ideal for most use cases. For situations where you need more control or are working with older C++ standards, we discussed creating a custom conversion function. Lastly, when dealing with extremely large numbers, we introduced the Boost.Multiprecision library to handle conversions.

Comments (0)

There are no comments. Be the first to comment!!!