Home > Net >  std::string characters somehow turned into Unicode/ASCII numbers
std::string characters somehow turned into Unicode/ASCII numbers

Time:12-19

I have a function ls() which parses a vector of string and puts it into a comma-separated list, wrapped within parentheses ():

std::string ls(std::vector<std::string> vec, std::string wrap="()", std::string sep=", ") {
    std::string wrap_open, wrap_close;
    wrap_open = std::to_string(wrap[0]);
    wrap_close = std::to_string(wrap[1]);
    std::string result = wrap_open;
    size_t length = vec.size();
    if (length > 0) {
        if (length == 1) {
            result  = vec[0];
            result  = wrap_close;
        }
        else {
            for (int i = 0; i < vec.size(); i  ) {
                if (i == vec.size() - 1) {
                    result  = sep;
                    result  = vec[i];
                    result  = wrap_close;
                }
                else if (i == 0) {
                    result  = vec[i];
                }
                else {
                    result  = sep;
                    result  = vec[i];
                }
            }
        }
    }
    else {
        result  = wrap_close;
    }

    return result;
}

If I pass this vector

std::vector<std::string> vec = {"hello", "world", "three"};

to the ls() function, I should get this string:

std::string parsed_vector = ls(vec);

// AKA

std::string result = "(hello, world, three)"

The parsing works fine, however the characters in the wrap string turn into numbers when printed.

std::cout << result << std::endl;

Will result in the following:

40hello, world, three41

When it should instead result in this:

(hello, world, three)

The ( is turned into 40, and the ) is turned into 41.

My guess is that the characters are being turned into the Unicode/ASCII number values or something like that, I do not know how this happened or what to do.

CodePudding user response:

The problem here is std::to_string converts a number to a string. There is no specialization for char values. So here, you're converting the ASCII value to a string:

wrap_open = std::to_string(wrap[0]);
wrap_close = std::to_string(wrap[1]);

Instead, you could simply do:

std::string wrap_open(1, wrap[0]);
std::string wrap_close(1, wrap[1]);

Note that you can greatly simplify your function by using std::ostringstream:

std::ostringstream oss;
oss << wrap[0];
for (size_t i = 0; i < vec.size(); i  )
{
    if (i != 0) oss << sep;
    oss << vec[i];
}
oss << wrap[1];
return oss.str();

CodePudding user response:

I won't be commenting on how you could improve the function and that passing a vector by value (as an argument in the function) is never a good idea, however I will tell you how to fix your current issue:

std::string ls(std::vector<std::string> vec, std::string wrap = "()", std::string sep = ", ") {
    std::string wrap_open, wrap_close;
    wrap_open = wrap.at(0); //<----
    wrap_close = wrap.at(1); //<----
    std::string result = wrap_open;
    size_t length = vec.size();
    if (length > 0) {
        if (length == 1) {
            result  = vec[0];
            result  = wrap_close;
        }
        else {
            for (int i = 0; i < vec.size(); i  ) {
                if (i == vec.size() - 1) {
                    result  = sep;
                    result  = vec[i];
                    result  = wrap_close;
                }
                else if (i == 0) {
                    result  = vec[i];
                }
                else {
                    result  = sep;
                    result  = vec[i];
                }
            }
        }
    }
    else {
        result  = wrap_close;
    }

    return result;
}

You don't need to use std::to_string, just use one of std::string's constructors to create a string with one character from the wrap string. This constructor is invoked via the = operator.

I recommend reading about std::string, it is apparent that you aren't using the full potential of the STL library : std::string

  • Related