1 '\" t -*- coding: UTF-8 -*-
2 .\" Copyright (c) Bruno Haible <haible@clisp.cons.org>
3 .\" and Copyright 2014 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\" %%%LICENSE_START(GPLv2+_DOC_ONEPARA)
6 .\" This is free documentation; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License as
8 .\" published by the Free Software Foundation; either version 2 of
9 .\" the License, or (at your option) any later version.
12 .\" References consulted:
13 .\" GNU glibc-2 source code and manual
14 .\" Dinkumware C library reference http://www.dinkumware.com/
15 .\" OpenGroup's Single UNIX specification http://www.UNIX-systems.org/online.html
18 .TH MBSTOWCS 3 2014-03-18 "GNU" "Linux Programmer's Manual"
20 mbstowcs \- convert a multibyte string to a wide-character string
23 .B #include <stdlib.h>
25 .BI "size_t mbstowcs(wchar_t *" dest ", const char *" src ", size_t " n );
36 to a wide-character string starting at
40 wide characters are written to
44 The conversion can stop for three reasons:
46 An invalid multibyte sequence has been encountered.
52 non-L\(aq\\0\(aq wide characters have been stored at
54 In this case, the number of wide characters written to
57 shift state at this point is lost.
59 The multibyte string has been completely converted, including the
60 terminating null wide character (\(aq\\0\(aq).
61 In this case, the number of wide characters written to
63 excluding the terminating null wide character, is returned.
65 The programmer must ensure that there is room for at least
75 is ignored, and the conversion proceeds as
76 above, except that the converted wide characters are not written out to memory,
77 and that no length limit exists.
79 In order to avoid the case 2 above, the programmer should make sure
82 greater than or equal to
83 .IR "mbstowcs(NULL,src,0)+1" .
87 function returns the number of wide characters that make
88 up the converted part of the wide-character string, not including the
89 terminating null wide character.
90 If an invalid multibyte sequence was
106 provides a better interface to the same
109 The program below illustrates the use of
111 as well as some of the wide character classification functions.
112 An example run is the following:
116 $ ./t_mbstowcs de_DE.UTF\-8 Grüße!
117 Length of source string (excluding terminator):
119 6 multibyte characters
121 Wide character string is: Grüße! (6 characters)
139 main(int argc, char *argv[])
141 size_t mbslen; /* Number of multibyte characters in source */
142 wchar_t *wcs; /* Pointer to converted wide character string */
146 fprintf(stderr, "Usage: %s <locale> <string>\\n", argv[0]);
150 /* Apply the specified locale */
152 if (setlocale(LC_ALL, argv[1]) == NULL) {
157 /* Calculate the length required to hold argv[2] converted to
158 a wide character string */
160 mbslen = mbstowcs(NULL, argv[2], 0);
161 if (mbslen == (size_t) \-1) {
166 /* Describe the source string to the user */
168 printf("Length of source string (excluding terminator):\\n");
169 printf(" %zu bytes\\n", strlen(argv[2]));
170 printf(" %zu multibyte characters\\n\\n", mbslen);
172 /* Allocate wide character string of the desired size. Add 1
173 to allow for terminating null wide character (L\(aq\\0\(aq). */
175 wcs = calloc(mbslen + 1, sizeof(wchar_t));
181 /* Convert the multibyte character string in argv[2] to a
182 wide character string */
184 if (mbstowcs(wcs, argv[2], mbslen + 1) == (size_t) \-1) {
189 printf("Wide character string is: %ls (%zu characters)\\n",
192 /* Now do some inspection of the classes of the characters in
193 the wide character string */
195 for (wp = wcs; *wp != 0; wp++) {
196 printf(" %lc ", (wint_t) *wp);
210 putchar(\(aq\\n\(aq);
223 This page is part of release 3.79 of the Linux
226 A description of the project,
227 information about reporting bugs,
228 and the latest version of this page,
230 \%http://www.kernel.org/doc/man\-pages/.