From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/string%2Fmultibyte%2Fwcsrtombs.html | 83 +++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 devdocs/c/string%2Fmultibyte%2Fwcsrtombs.html (limited to 'devdocs/c/string%2Fmultibyte%2Fwcsrtombs.html') diff --git a/devdocs/c/string%2Fmultibyte%2Fwcsrtombs.html b/devdocs/c/string%2Fmultibyte%2Fwcsrtombs.html new file mode 100644 index 00000000..fd435f8a --- /dev/null +++ b/devdocs/c/string%2Fmultibyte%2Fwcsrtombs.html @@ -0,0 +1,83 @@ +

wcsrtombs, wcsrtombs_s

Defined in header <wchar.h>
(1)
size_t wcsrtombs( char *dst, const wchar_t **src, size_t len, mbstate_t* ps );
+(since C95)
(until C99) +
size_t wcsrtombs( char *restrict dst, const wchar_t **restrict src, size_t len,
+                  mbstate_t *restrict ps );
+
(since C99)
errno_t wcsrtombs_s( size_t *restrict retval, char *restrict dst, rsize_t dstsz,
+                     const wchar_t **restrict src, rsize_t len,
+                     mbstate_t *restrict ps );
+
(2) (since C11)
1) Converts a sequence of wide characters from the array whose first element is pointed to by *src to its narrow multibyte representation that begins in the conversion state described by *ps. If dst is not null, converted characters are stored in the successive elements of the char array pointed to by dst. No more than len bytes are written to the destination array. Each character is converted as if by a call to wcrtomb. The conversion stops if: 2) Same as (1), except that
+
+
    +
  • retval, ps, src, or *src is a null pointer
  • +
  • dstsz or len is greater than RSIZE_MAX (unless dst is null)
  • +
  • dstsz is not zero (unless dst is null)
  • +
  • len is greater than dstsz and the conversion does not encounter null or encoding error in the src array by the time dstsz is reached (unless dst is null)
  • +
+
As with all bounds-checked functions, wcsrtombs_s only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <wchar.h>.
+

Parameters

+ + + + + + +
dst - pointer to narrow character array where the multibyte characters will be stored
src - pointer to pointer to the first element of a null-terminated wide string
len - number of bytes available in the array pointed to by dst
ps - pointer to the conversion state object
dstsz - max number of bytes that will be written (size of the dst array)
retval - pointer to a size_t object where the result will be stored

Return value

+1) On success, returns the number of bytes (including any shift sequences, but excluding the terminating '\0') written to the character array whose first element is pointed to by dst. If dst is a null pointer, returns the number of bytes that would have been written. On conversion error (if invalid wide character was encountered), returns (size_t)-1, stores EILSEQ in errno, and leaves *ps in unspecified state.
+2) Returns zero on success (in which case the number of bytes excluding terminating zero that were, or would be written to dst, is stored in *retval), non-zero on error. In case of a runtime constraint violation, stores (size_t)-1 in *retval (unless retval is null) and sets dst[0] to '\0' (unless dst is null or dstmax is zero or greater than RSIZE_MAX)

Example

#include <stdio.h>
+#include <locale.h>
+#include <string.h>
+#include <wchar.h>
+ 
+void print_wide(const wchar_t* wstr)
+{
+    mbstate_t state;
+    memset(&state, 0, sizeof state);
+    size_t len = 1 + wcsrtombs(NULL, &wstr, 0, &state);
+    char mbstr[len];
+    wcsrtombs(mbstr, &wstr, len, &state);
+    printf("Multibyte string: %s\n", mbstr);
+    printf("Length, including '\\0': %zu\n", len);
+}
+ 
+int main(void)
+{
+    setlocale(LC_ALL, "en_US.utf8");
+    print_wide(L"z\u00df\u6c34\U0001f34c"); // or L"zß水🍌"
+}

Output:

+
Multibyte string: zß水🍌
+Length, including '\0': 11

References

See also

+ + + +
+
(C11)
converts a wide string to narrow multibyte character string
(function)
+
(C95)(C11)
converts a wide character to its multibyte representation, given state
(function)
+
(C95)(C11)
converts a narrow multibyte character string to wide string, given state
(function)
C++ documentation for wcsrtombs
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/string/multibyte/wcsrtombs +

+
-- cgit v1.2.3