The C++ standard library
is a header file in the C++ standard library that provides character conversion tools. This header mainly contains std::codecvt class templates and their specializations, supporting conversion between character encodings, such as from UTF-8 to UTF-16, or from wide characters (wchar_t) to narrow characters (char), and so on.std::codecvt The class is usually used together with std::wstring_convert class to perform character encoding conversion.
Syntax
codecvt The main classes and functions in the namespace are as follows:
codecvt_base: Defines the state type and error handling method for encoding conversion.codecvt_byname: Template class for creating converters for specific encodings.codecvt_utf8,codecvt_utf16: Converter class for specific encodings.
Basic Syntax
#include
#include
#include
std::wstring_convert> converter;
std::wstring wide_string = converter.from_bytes("Hello, World!");
std::string narrow_string = converter.to_bytes(L"你好,世界!");
Example
Example 1: UTF-8 to UTF-16 conversion
In this example, we will demonstrate how to use codecvt to convert a UTF-8 encoded string to a UTF-16 encoded wide string.
Example
#include
#include
#include
#include
int
main
(
)
{
// Create a UTF-8 to UTF-16 converter
std
::
wstring_convert
<
std
::
codecvt_utf8_utf16
<
wchar_t
converter;
// Original UTF-8 string
std
::
string
narrow_string
=
"Hello, World!"
;
// Convert to UTF-16 wide string
std
::
wstring
wide_string
=
converter.
from_bytes
(
narrow_string
)
;
// Output the wide string
std
::
wcout
<<
L
"Wide string: "
<<
wide_string
<<
std
::
endl
;
// Convert the wide string back to a UTF-8 string
std
::
string
converted_string
=
converter.
to_bytes
(
wide_string
)
;
// Output the converted string
std
::
cout
<<
"Converted string: "
<<
converted_string
<<
std
::
endl
;
return
0
;
}
Output result:
Wide string: Hello, World!
Converted string: Hello, World!
Example 2: Using codecvt_byname for encoding conversion
In this example, we will demonstrate how to use codecvt_byname class to create a name-based encoding converter and use it for conversion.
Example
#include
#include
#include
#include
int
main
(
)
{
// Create a name-based converter; here "zh_CN.UTF-8" represents the UTF-8 encoding of Simplified Chinese
std
::
wstring_convert
<
std
::
codecvt_byname
<
wchar_t
converter
(
"zh_CN.UTF-8"
);
// Original UTF-8 string
std
::
string
narrow_string
=
"Hello, World!"
;
// Convert to wide string
std
::
wstring
wide_string
=
converter.
from_bytes
(
narrow_string
)
;
// Output the wide string
std
::
wcout
<<
L
"Wide string: "
<<
wide_string
<<
std
::
endl
;
// Convert the wide string back to a UTF-8 string
std
::
string
converted_string
=
converter.
to_bytes
(
wide_string
)
;
// Output the converted string
std
::
cout
<<
"Converted string: "
<<
converted_string
<<
std
::
endl
;
return
0
;
}
Output result:
Wide string: 你好,世界!
Converted string: 你好,世界!
std::codecvt Class template specializations
std::codecvt has several specializations for different character encoding conversions:
std::codecvt_utf8: Wide characters (wchar_t) to UTF-8 conversion.std::codecvt_utf8_utf16: UTF-8 to UTF-16 conversion.std::codecvt_utf8: UTF-8 to UTF-32 conversion.
std::wstring_convert Class template
std::wstring_convert class template is a helper class for managing the lifecycle and exception handling of character encoding conversions:
to_bytes: Converts a wide-character or other-encoded string to a narrow-character (byte sequence).from_bytes: Converts a narrow-character (byte sequence) to a wide-character or other-encoded string.
Notes
- In the C++17 standard,
std::codecvthas been deprecated; it is recommended to use other alternatives (such as the ICU library) for character encoding conversion in the future. - For cross-platform applications, you should be especially careful when handling character encodings to ensure consistent behavior across all platforms.
Summary
provides a powerful set of tools for conversion between different character encodings, especially between UTF-8, UTF-16, and wide characters. Although deprecated in C++17, it remains a useful tool for handling character encoding conversion. Understanding and mastering these tools can help you write more flexible and internationalized applications. If you have specific usage needs or questions, we can discuss further.