Namespaces
Variants
Views
Actions

Difference between revisions of "c/string/multibyte"

From cppreference.com
< c‎ | string
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh)
 
(17 intermediate revisions by 5 users not shown)
Line 6: Line 6:
 
Each character stored in the string may occupy more than one byte. The encoding used to represent characters in a multibyte character string is locale-specific: it may be UTF-8, GB18030, EUC-JP, Shift-JIS, etc. For example, the char array {{c|{'\xe4','\xbd','\xa0','\xe5','\xa5','\xbd','\0'}}} is an NTMBS holding the string {{c|"你好"}} in UTF-8 multibyte encoding: the first three bytes encode the character 你, the next three bytes encode the character 好. The same string encoded in GB18030 is the char array {{c|{'\xc4', '\xe3', '\xba', '\xc3', '\0'}}}, where each of the two characters is encoded as a two-byte sequence.
 
Each character stored in the string may occupy more than one byte. The encoding used to represent characters in a multibyte character string is locale-specific: it may be UTF-8, GB18030, EUC-JP, Shift-JIS, etc. For example, the char array {{c|{'\xe4','\xbd','\xa0','\xe5','\xa5','\xbd','\0'}}} is an NTMBS holding the string {{c|"你好"}} in UTF-8 multibyte encoding: the first three bytes encode the character 你, the next three bytes encode the character 好. The same string encoded in GB18030 is the char array {{c|{'\xc4', '\xe3', '\xba', '\xc3', '\0'}}}, where each of the two characters is encoded as a two-byte sequence.
  
In some multibyte encodings, any given multibyte character sequence may represent different characters depending on the previous byte sequences, known as "shift sequences". Such encodings are known as state-dependent: knowledge of the current shift state is required to interpret each character. An NTMBS is only valid if it begins and ends in the initial shift state: if a shift sequence was used, the corresponding unshift sequence has to be present before the terminating null character. Examples of such encodings are BOCU-1 and SCSU.
+
In some multibyte encodings, any given multibyte character sequence may represent different characters depending on the previous byte sequences, known as "shift sequences". Such encodings are known as state-dependent: knowledge of the current shift state is required to interpret each character. An NTMBS is only valid if it begins and ends in the initial shift state: if a shift sequence was used, the corresponding unshift sequence has to be present before the terminating null character. Examples of such encodings are BOCU-1 and [http://www.unicode.org/reports/tr6 SCSU].
  
A multibyte character string is layout-compatible with null-terminated byte string (NTBS), that is, can be stored, copied, and examined using the same facilities, except for calculating the number of characters. If the correct locale is in effect, I/O functions also handle multibyte strings. Multibyte strings can be converted to and from wide strings using the {{c|codecvt}} member functions, {{c|wstring_convert}}, or the following locale-dependent conversion functions:
+
A multibyte character string is layout-compatible with [[c/string/byte|null-terminated byte string]] (NTBS), that is, can be stored, copied, and examined using the same facilities, except for calculating the number of characters. If the correct locale is in effect, I/O functions also handle multibyte strings. Multibyte strings can be converted to and from wide strings using the following locale-dependent conversion functions:
  
 
===Multibyte/wide character conversions===
 
===Multibyte/wide character conversions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list header | stdlib.h}}
+
{{dsc header | stdlib.h}}
{{dcl list template | c/string/multibyte/dcl list mblen}}
+
{{dsc inc | c/string/multibyte/dsc mblen}}
{{dcl list template | c/string/multibyte/dcl list mbtowc}}
+
{{dsc inc | c/string/multibyte/dsc mbtowc}}
{{dcl list template | c/string/multibyte/dcl list wctomb}}
+
{{dsc inc | c/string/multibyte/dsc wctomb}}
{{dcl list template | c/string/multibyte/dcl list mbstowcs}}
+
{{dsc inc | c/string/multibyte/dsc mbstowcs}}
{{dcl list template | c/string/multibyte/dcl list wcstombs}}
+
{{dsc inc | c/string/multibyte/dsc wcstombs}}
{{dcl list header | wchar.h}}
+
{{dsc header | wchar.h}}
{{dcl list template | c/string/multibyte/dcl list mbsinit}}
+
{{dsc inc | c/string/multibyte/dsc mbsinit}}
{{dcl list template | c/string/multibyte/dcl list btowc}}
+
{{dsc inc | c/string/multibyte/dsc btowc}}
{{dcl list template | c/string/multibyte/dcl list wctob}}
+
{{dsc inc | c/string/multibyte/dsc wctob}}
{{dcl list template | c/string/multibyte/dcl list mbrlen}}
+
{{dsc inc | c/string/multibyte/dsc mbrlen}}
{{dcl list template | c/string/multibyte/dcl list mbrtowc}}
+
{{dsc inc | c/string/multibyte/dsc mbrtowc}}
{{dcl list template | c/string/multibyte/dcl list wcrtomb}}
+
{{dsc inc | c/string/multibyte/dsc wcrtomb}}
{{dcl list template | c/string/multibyte/dcl list mbsrtowcs}}
+
{{dsc inc | c/string/multibyte/dsc mbsrtowcs}}
{{dcl list template | c/string/multibyte/dcl list wcsrtombs}}
+
{{dsc inc | c/string/multibyte/dsc wcsrtombs}}
{{dcl list header | uchar.h}}
+
{{dsc header | uchar.h}}
{{dcl list template | c/string/multibyte/dcl list mbrtoc16}}
+
{{dsc inc | c/string/multibyte/dsc mbrtoc8}}
{{dcl list template | c/string/multibyte/dcl list c16rtomb}}
+
{{dsc inc | c/string/multibyte/dsc c8rtomb}}
{{dcl list template | c/string/multibyte/dcl list mbrtoc32}}
+
{{dsc inc | c/string/multibyte/dsc mbrtoc16}}
{{dcl list template | c/string/multibyte/dcl list c32rtomb}}
+
{{dsc inc | c/string/multibyte/dsc c16rtomb}}
{{dcl list end}}
+
{{dsc inc | c/string/multibyte/dsc mbrtoc32}}
 +
{{dsc inc | c/string/multibyte/dsc c32rtomb}}
 +
{{dsc end}}
  
 
===Types===
 
===Types===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list header | wchar.h}}
+
{{dsc header | wchar.h}}
{{dcl list template | c/string/multibyte/dcl list mbstate_t}}
+
{{dsc inc | c/string/multibyte/dsc mbstate_t}}
{{dcl list header | uchar.h}}
+
{{dsc header | uchar.h}}
{{dcl list template | c/string/multibyte/dcl list char16_t}}
+
{{dsc inc | c/string/multibyte/dsc char8_t}}
{{dcl list template | c/string/multibyte/dcl list char32_t}}
+
{{dsc inc | c/string/multibyte/dsc char16_t}}
{{dcl list end}}
+
{{dsc inc | c/string/multibyte/dsc char32_t}}
 +
{{dsc end}}
  
 
===Macros===
 
===Macros===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list header | limits.h}}
+
{{dsc header | limits.h}}
{{dcl list template | c/string/multibyte/dcl list MB_LEN_MAX}}
+
{{dsc inc | c/string/multibyte/dsc MB_LEN_MAX}}
{{dcl list header | stdlib.h}}
+
{{dsc header | stdlib.h}}
{{dcl list template | c/string/multibyte/dcl list MB_CUR_MAX}}
+
{{dsc inc | c/string/multibyte/dsc MB_CUR_MAX}}
{{dcl list header | uchar.h}}
+
{{dsc end}}
{{dcl list macro const | __STDC_UTF_16__ |  nolink=true | indicates that UTF-16 encoding is used by {{c|mbrtoc16}} and {{c|c16rtomb}} | notes={{mark c11}}}}
+
{{dcl list macro const | __STDC_UTF_32__ |  nolink=true | indicates that UTF-32 encoding is used by {{c|mbrtoc32}} and {{c|c32rtomb}}} | notes={{mark c11}}}}
+
{{dcl list end}}
+
  
 +
===References===
 +
{{ref std c11}}
 +
{{ref std | section=7.10    | title=Sizes of integer types <limits.h>| p=222}}
 +
{{ref std | section=7.22    | title=General utilities <stdlib.h> | p=340-360}}
 +
{{ref std | section=7.28    | title=Unicode utilities <uchar.h> | p=398-401}}
 +
{{ref std | section=7.29    | title=Extended multibyte and wide character utilities <wchar.h> | p=402-446}}
 +
{{ref std | section=7.31.12 | title=General utilities <stdlib.h> | p=456}}
 +
{{ref std | section=7.31.16 | title=Extended multibyte and wide character utilities <wchar.h> | p=456}}
 +
{{ref std | section=K.3.6  | title=General utilities <stdlib.h> | p=604-614}}
 +
{{ref std | section=K.3.9  | title=Extended multibyte and wide character utilities <wchar.h> | p=627-651}}
 +
{{ref std c99}}
 +
{{ref std | section=7.10    | title=Sizes of integer types <limits.h>| p=203}}
 +
{{ref std | section=7.20    | title=General utilities <stdlib.h> | p=306-324}}
 +
{{ref std | section=7.24    | title=Extended multibyte and wide character utilities <wchar.h> | p=348-392}}
 +
{{ref std | section=7.26.10 | title=General utilities <stdlib.h> | p=402}}
 +
{{ref std | section=7.26.12 | title=Extended multibyte and wide character utilities <wchar.h> | p=402}}
 +
{{ref std c89}}
 +
{{ref std | section=4.1.4  | title=Limits <float.h> and <limits.h>}}
 +
{{ref std | section=4.10  | title=GENERAL UTILITIES <stdlib.h>}}
 +
{{ref std | section=4.13.7 | title=General utilities <stdlib.h>}}
 +
{{ref std end}}
 +
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc see cpp | cpp/string/multibyte | Null-terminated multibyte strings}}
 +
{{dsc end}}
 +
 +
[[ar:c/string/multibyte]]
 +
[[cs:c/string/multibyte]]
 
[[de:c/string/multibyte]]
 
[[de:c/string/multibyte]]
 
[[es:c/string/multibyte]]
 
[[es:c/string/multibyte]]
Line 59: Line 88:
 
[[it:c/string/multibyte]]
 
[[it:c/string/multibyte]]
 
[[ja:c/string/multibyte]]
 
[[ja:c/string/multibyte]]
 +
[[ko:c/string/multibyte]]
 +
[[pl:c/string/multibyte]]
 
[[pt:c/string/multibyte]]
 
[[pt:c/string/multibyte]]
 
[[ru:c/string/multibyte]]
 
[[ru:c/string/multibyte]]
 +
[[tr:c/string/multibyte]]
 
[[zh:c/string/multibyte]]
 
[[zh:c/string/multibyte]]

Latest revision as of 17:51, 19 January 2023

A null-terminated multibyte string (NTMBS), or "multibyte string", is a sequence of nonzero bytes followed by a byte with value zero (the terminating null character).

Each character stored in the string may occupy more than one byte. The encoding used to represent characters in a multibyte character string is locale-specific: it may be UTF-8, GB18030, EUC-JP, Shift-JIS, etc. For example, the char array {'\xe4','\xbd','\xa0','\xe5','\xa5','\xbd','\0'} is an NTMBS holding the string "你好" in UTF-8 multibyte encoding: the first three bytes encode the character 你, the next three bytes encode the character 好. The same string encoded in GB18030 is the char array {'\xc4', '\xe3', '\xba', '\xc3', '\0'}, where each of the two characters is encoded as a two-byte sequence.

In some multibyte encodings, any given multibyte character sequence may represent different characters depending on the previous byte sequences, known as "shift sequences". Such encodings are known as state-dependent: knowledge of the current shift state is required to interpret each character. An NTMBS is only valid if it begins and ends in the initial shift state: if a shift sequence was used, the corresponding unshift sequence has to be present before the terminating null character. Examples of such encodings are BOCU-1 and SCSU.

A multibyte character string is layout-compatible with null-terminated byte string (NTBS), that is, can be stored, copied, and examined using the same facilities, except for calculating the number of characters. If the correct locale is in effect, I/O functions also handle multibyte strings. Multibyte strings can be converted to and from wide strings using the following locale-dependent conversion functions:

Contents

[edit] Multibyte/wide character conversions

Defined in header <stdlib.h>
returns the number of bytes in the next multibyte character
(function) [edit]
converts the next multibyte character to wide character
(function) [edit]
converts a wide character to its multibyte representation
(function) [edit]
converts a narrow multibyte character string to wide string
(function) [edit]
converts a wide string to narrow multibyte character string
(function) [edit]
Defined in header <wchar.h>
checks if the mbstate_t object represents initial shift state
(function) [edit]
(C95)
widens a single-byte narrow character to wide character, if possible
(function) [edit]
(C95)
narrows a wide character to a single-byte narrow character, if possible
(function) [edit]
(C95)
returns the number of bytes in the next multibyte character, given state
(function) [edit]
converts the next multibyte character to wide character, given state
(function) [edit]
converts a wide character to its multibyte representation, given state
(function) [edit]
converts a narrow multibyte character string to wide string, given state
(function) [edit]
converts a wide string to narrow multibyte character string, given state
(function) [edit]
Defined in header <uchar.h>
converts a narrow multibyte character to UTF-8 encoding
(function) [edit]
converts UTF-8 string to narrow multibyte encoding
(function) [edit]
generates the next 16-bit wide character from a narrow multibyte string
(function) [edit]
converts a 16-bit wide character to narrow multibyte string
(function) [edit]
generates the next 32-bit wide character from a narrow multibyte string
(function) [edit]
converts a 32-bit wide character to narrow multibyte string
(function) [edit]

[edit] Types

Defined in header <wchar.h>
conversion state information necessary to iterate multibyte character strings
(class) [edit]
Defined in header <uchar.h>
UTF-8 character type, an alias for unsigned char
(typedef) [edit]
16-bit wide character type
(typedef) [edit]
32-bit wide character type
(typedef) [edit]

[edit] Macros

Defined in header <limits.h>
MB_LEN_MAX
maximum number of bytes in a multibyte character, for any supported locale
(macro constant) [edit]
Defined in header <stdlib.h>
MB_CUR_MAX
maximum number of bytes in a multibyte character, in the current locale
(macro variable) [edit]

[edit] References

  • C11 standard (ISO/IEC 9899:2011):
  • 7.10 Sizes of integer types <limits.h> (p: 222)
  • 7.22 General utilities <stdlib.h> (p: 340-360)
  • 7.28 Unicode utilities <uchar.h> (p: 398-401)
  • 7.29 Extended multibyte and wide character utilities <wchar.h> (p: 402-446)
  • 7.31.12 General utilities <stdlib.h> (p: 456)
  • 7.31.16 Extended multibyte and wide character utilities <wchar.h> (p: 456)
  • K.3.6 General utilities <stdlib.h> (p: 604-614)
  • K.3.9 Extended multibyte and wide character utilities <wchar.h> (p: 627-651)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.10 Sizes of integer types <limits.h> (p: 203)
  • 7.20 General utilities <stdlib.h> (p: 306-324)
  • 7.24 Extended multibyte and wide character utilities <wchar.h> (p: 348-392)
  • 7.26.10 General utilities <stdlib.h> (p: 402)
  • 7.26.12 Extended multibyte and wide character utilities <wchar.h> (p: 402)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 4.1.4 Limits <float.h> and <limits.h>
  • 4.10 GENERAL UTILITIES <stdlib.h>
  • 4.13.7 General utilities <stdlib.h>

[edit] See also

C++ documentation for Null-terminated multibyte strings