Sai A Sai A
Updated date Nov 27, 2023
In this blog, we will explore how to convert ASCII to String in C++. From basic arithmetic to advanced stream handling methods.

Introduction:

ASCII, the backbone of character encoding, converts characters into numerical values in the programming world. Converting these numeric values back into human-readable characters is a fundamental skill in programming. This blog aims to explain the process of converting ASCII to a string in C++. We will explore various methods, providing step-by-step explanations along with code snippets and outputs for each method.

Method 1: Simple Arithmetic Operations

Let's start with a basic arithmetic approach. Each character in ASCII is represented by a unique number, allowing for simple conversion using arithmetic operations.

#include <iostream>
#include <string>

int main() {
    int asciiValues[] = {72, 101, 108, 108, 111};
    std::string result;

    for (int i : asciiValues) {
        result += static_cast<char>(i);
    }

    std::cout << "Method 1 Output: " << result << std::endl;

    return 0;
}

In this method, we have an array asciiValues representing the ASCII values of the characters 'H', 'e', 'l', 'l', and 'o'. The for loop iterates through each value, converts it to a character using static_cast<char>(i), and appends it to the result string. The final converted string is then printed.

Output:

Method 1 Output: Hello

Method 2: Using the stringstream Class

Utilizing the std::stringstream class provides a stream-based approach for ASCII to string conversion.

#include <iostream>
#include <string>
#include <sstream>

int main() {
    int asciiValues[] = {87, 111, 114, 108, 100};
    std::stringstream ss;

    for (int i : asciiValues) {
        ss << static_cast<char>(i);
    }

    std::string result = ss.str();

    std::cout << "Method 2 Output: " << result << std::endl;

    return 0;
}

In this method, a std::stringstream object ss is used to build the string. The loop converts each ASCII value to a character and appends it to the stream. The ss.str() function retrieves the final string from the stream.

Output:

Method 2 Output: World

Method 3: Using the C++11 Range-based for Loop

C++11 introduced the range-based for loop, simplifying the iteration through elements of a container.

#include <iostream>
#include <string>

int main() {
    int asciiValues[] = {65, 83, 67, 73, 73};
    std::string result;

    for (auto i : asciiValues) {
        result += static_cast<char>(i);
    }

    std::cout << "Method 3 Output: " << result << std::endl;

    return 0;
}

The range-based for loop simplifies the syntax for iterating through the asciiValues array. ASCII values are converted to characters and appended to the result string.

Output:

Method 3 Output: ASCII

Conclusion:

In this blog, we have explored various methods to convert ASCII to a string in C++. From simple arithmetic operations to using C++ features like std::stringstream and the range-based for loop, each method has its strengths.

Comments (0)

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