user81993
user81993

Reputation: 6613

Is there any built in support for UTF in c++ in windows?

I need to convert some utf8 encoded numbers into floats in c++ using VS2013. Is there anything in the standard library or provided by microsoft headers that would help me do that?

Alternatively, I hear that utf8 should be compatible with ASCII, is there anything for that?

Upvotes: 0

Views: 89

Answers (2)

marcinj
marcinj

Reputation: 50046

You can use MultiByteToWideChar WinAPI function, below is example code.

int UTF8toUTF16(const CHAR* utf8, WCHAR* utf16) {
    int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    if (utf16 == NULL)
        return len;
    if (len>1) {
        return MultiByteToWideChar(CP_UTF8, 0, utf8, -1, utf16, len);
    }
    return 0;
}


const CHAR* utf8str = "someutf8string";

int requiredLen = UTF8toUTF16(utf8str, nullptr);
if (requiredLen > 0) {
    std::vector<WCHAR> utf16str(requiredLen, '\0');
    UTF8toUTF16(utf8str.data(), &utf16str.front());
    // do something with data
}

if you numbers are plain ASCII then of course this conversion will do nothing, but if your requirement says text on input is in UTF8 then to be safe you should do such conversion, at least I would do it.

for further conversion look into here : atoi() with other languages

Upvotes: 2

Hans Kl&#252;nder
Hans Kl&#252;nder

Reputation: 2292

Don't panic. For all digits and for all other characters used in floating numbers, UTF8 is the same as ASCII.

UTF8 represents unicode characters by sequences of bytes. These sequences have variable length. For all unicode characters below 128, the sequence is just one byte containing that character. Thus for you there is no difference between UTF8 and ASCII.

You can use the standard methods and ignore that the input is UTF8.

Upvotes: 3

Related Questions