Exception wstring_converter while parsing c string

I have the following code:

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


int main()
{
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;

    const char val[] = "+3°C";
    wstring text = converter.from_bytes(val);

    return 0;
}

      

The problem is that the method is converter.from_bytes

throwing an exception. What for? How should I parse a given string?

The only exception is the type std::range_error

with the message

bad conversion

enter image description here

The problem is with the '°' character, as if I remove that character the conversion is fine.

+3


source to share


1 answer


I am assuming that the string literal is "+3°C"

not UTF-8 encoded because your IDE uses a different original character set.

You can embed a character °

directly into the source code if the source file itself is UTF-8 encoded. If it uses some Windows codepage that represents °

differently, then it is probably inserting one or more bytes into the string that are not valid UTF-8 characters, so the conversion from UTF-8 to UTF-16 fails.

It works great in a live demo like http://coliru.stacked-crooked.com/a/23923c288ed5f9f3 because it works on a different OS where the compiler assumes the source files are using UTF-8 by default (which is the standard for GNU / Linux and other platforms with better handling of non-ASCII text).



Try replacing it with a UTF-8 literal u8"+3\u2103"

(using the generic character name for the DEGREES CELSIUS character ) or u8"+3\u00B0C"

(using the generic character name for the DEGREE SIGN character and then capital C).

This tells the compiler that you want a string containing the UTF-8 representation of exactly those Unicode characters, regardless of the encoding of the original file itself.

+3


source







All Articles