Home >Backend Development >C++ >How Can I Safely Convert a UTF-8 String to a wstring in C ?

How Can I Safely Convert a UTF-8 String to a wstring in C ?

Linda Hamilton
Linda HamiltonOriginal
2024-12-18 03:04:09126browse

How Can I Safely Convert a UTF-8 String to a wstring in C  ?

C : Converting a string to a wstring while Preserving Encoding

You are given a string s containing Japanese characters and wish to assign its contents to a wstring, ws. The task is to perform this assignment without introducing any encoding issues.

Solution:

Utilizing C 11 and above, this task can be accomplished solely with the standard library.

Step 1: Include Necessary Headers

#include <locale>
#include <codecvt>
#include <string>

Step 2: Create a converter

std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;

This converter will handle the conversion between UTF-8 (the encoding of s) and UTF-16 (the encoding of ws).

Step 3: Convert the string to a wstring

std::wstring ws = converter.from_bytes(s);

Example:

std::string s = "おはよう";
std::wstring ws;

std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
ws = converter.from_bytes(s);

Note:

  • This solution assumes s contains valid UTF-8 encoded Unicode data.
  • For more complex encoding scenarios, consider using std::codecvt_utf8.

The above is the detailed content of How Can I Safely Convert a UTF-8 String to a wstring in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn