Wojciech Danilo
Wojciech Danilo

Reputation: 11803

How to convert u32string to int in C++11?

How can we convert u32string to int in C++11?

Additional, what method should I use to convert part of such string to int - lets say having begin and end iterator available?

I've tried:

u32string test=U"14";
cout << atoi(test.c_str());

but it throws:

candidate function not viable: no known conversion from 'const char32_t *' to 'const char *' for 1st argument
extern int atoi (__const char *__nptr)

Upvotes: 8

Views: 5493

Answers (3)

rustyx
rustyx

Reputation: 85452

There is no standard library function for this, but thankfully it's quite easy to implement.

Here's an example of how to do it in a generic way and without costly conversions to std::string.

#include <string>
#include <iostream>

template<typename Out, typename In>
Out parse_int(In const& in)
{
    Out res = 0;
    std::size_t start = 0;
    while (std::isspace(in[start])) {
        start++;
    }
    std::size_t i = start;
    if (in[i] == '-') {
        i++;
    }
    while (std::isdigit(in[i])) {
        res = res * 10 + in[i++] - '0';
    }

    return in[start] == '-' ? -res : res;
}

int main()
{
    std::u32string s{ '1','2','3','4','5' };

    std::cout << parse_int<long long>(s) << std::endl;
}

The parse_int function works with u16string, u32string, u8string, string and wstring, exploiting the fact that digits are represented using the same values in all character types.

Live demo

Upvotes: 0

bames53
bames53

Reputation: 88215

#include <locale>   // wstring_convert
#include <codecvt>  // codecvt_utf8
#include <iostream> // cout
#include <string>   // stoi and u32string

int main() {
  std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> convert;

  std::u32string str = U"14";
  std::cout << std::stoi(convert.to_bytes(str));
}

This depends on UTF-8 and the "C" locale using the same representation for digits.


GCC's standard library implementation libstdc++ does not include the codecvt header or std::wstring_convert yet. libc++ does include both of these, as does Visual Studio's standard library implementation. If you have to use libstdc++ you may find it easiest to just implement a simple conversion function yourself.

#include <algorithm> // transform
#include <iterator>  // begin, end, and back_inserter

std::string u32_to_ascii(std::u32string const &s) {
  std::string out;
  std::transform(begin(s), end(s), back_inserter(out), [](char32_t c) {
    return c < 128 ? static_cast<char>(c) : '?';
  });
  return out;
}

int u32toi(std::u32string const &s) { return stoi(u32_to_ascii(s)); }

Upvotes: 10

Xaqq
Xaqq

Reputation: 4386

Edited because my first answer was stupid.

Here is what i managed to do, however its probably not very efficient, and it assumes your string is valid.

#include <string>
#include <iostream>

int     main()
{
  std::u32string str = U"14";

  std::string res;
  for (auto c = str.begin(); c != str.end(); ++c)
    {
      char t = *c;
      res.push_back(t);
    }
  std::cout << "\nVal = " << atoi(res.c_str()) << std::endl;
  return (0);
}

Upvotes: 0

Related Questions