322 lines
11 KiB
HTML
322 lines
11 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of MBSTOWCS</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>MBSTOWCS</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
mbstowcs - convert a multibyte string to a wide-character string
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>></B>
|
|
|
|
<B>size_t mbstowcs(wchar_t *</B><I>dest</I><B>, const char *</B><I>src</I><B>, size_t </B><I>n</I><B>);</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
If
|
|
<I>dest</I>
|
|
|
|
is not NULL,
|
|
the
|
|
<B>mbstowcs</B>()
|
|
|
|
function converts the
|
|
multibyte string
|
|
<I>src</I>
|
|
|
|
to a wide-character string starting at
|
|
<I>dest</I>.
|
|
|
|
At most
|
|
<I>n</I>
|
|
|
|
wide characters are written to
|
|
<I>dest</I>.
|
|
|
|
The sequence of characters in the string
|
|
<I>src</I>
|
|
|
|
shall begin in the initial shift state.
|
|
The conversion can stop for three reasons:
|
|
<DL COMPACT>
|
|
<DT id="1">1.<DD>
|
|
An invalid multibyte sequence has been encountered.
|
|
In this case,
|
|
<I>(size_t) -1</I>
|
|
|
|
is returned.
|
|
<DT id="2">2.<DD>
|
|
<I>n</I>
|
|
|
|
non-L'\0' wide characters have been stored at
|
|
<I>dest</I>.
|
|
|
|
In this case, the number of wide characters written to
|
|
<I>dest</I>
|
|
|
|
is returned, but the
|
|
shift state at this point is lost.
|
|
<DT id="3">3.<DD>
|
|
The multibyte string has been completely converted, including the
|
|
terminating null character ('\0').
|
|
In this case, the number of wide characters written to
|
|
<I>dest</I>,
|
|
|
|
excluding the terminating null wide character, is returned.
|
|
</DL>
|
|
<P>
|
|
|
|
The programmer must ensure that there is room for at least
|
|
<I>n</I>
|
|
|
|
wide
|
|
characters at
|
|
<I>dest</I>.
|
|
|
|
<P>
|
|
|
|
If
|
|
<I>dest</I>
|
|
|
|
is NULL,
|
|
<I>n</I>
|
|
|
|
is ignored, and the conversion proceeds as
|
|
above, except that the converted wide characters are not written out to memory,
|
|
and that no length limit exists.
|
|
<P>
|
|
|
|
In order to avoid the case 2 above, the programmer should make sure
|
|
<I>n</I>
|
|
|
|
is
|
|
greater than or equal to
|
|
<I>mbstowcs(NULL,src,0)+1</I>.
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
The
|
|
<B>mbstowcs</B>()
|
|
|
|
function returns the number of wide characters that make
|
|
up the converted part of the wide-character string, not including the
|
|
terminating null wide character.
|
|
If an invalid multibyte sequence was
|
|
encountered,
|
|
<I>(size_t) -1</I>
|
|
|
|
is returned.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>mbstowcs</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<A NAME="lbAG"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008, C99.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
The behavior of
|
|
<B>mbstowcs</B>()
|
|
|
|
depends on the
|
|
<B>LC_CTYPE</B>
|
|
|
|
category of the
|
|
current locale.
|
|
<P>
|
|
|
|
The function
|
|
<B><A HREF="/cgi-bin/man/man2html?3+mbsrtowcs">mbsrtowcs</A></B>(3)
|
|
|
|
provides a better interface to the same
|
|
functionality.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The program below illustrates the use of
|
|
<B>mbstowcs</B>(),
|
|
|
|
as well as some of the wide character classification functions.
|
|
An example run is the following:
|
|
<P>
|
|
|
|
|
|
|
|
$ ./t_mbstowcs de_DE.UTF-8 Grüße!
|
|
Length of source string (excluding terminator):
|
|
<BR> 8 bytes
|
|
<BR> 6 multibyte characters
|
|
<P>
|
|
Wide character string is: Grüße! (6 characters)
|
|
<BR> G alpha upper
|
|
<BR> r alpha lower
|
|
<BR> ü alpha lower
|
|
<BR> ß alpha lower
|
|
<BR> e alpha lower
|
|
<BR> ! !alpha
|
|
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/wctype.h">wctype.h</A>>
|
|
#include <<A HREF="file:///usr/include/locale.h">locale.h</A>>
|
|
#include <<A HREF="file:///usr/include/wchar.h">wchar.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> size_t mbslen; /* Number of multibyte characters in source */
|
|
<BR> wchar_t *wcs; /* Pointer to converted wide character string */
|
|
<BR> wchar_t *wp;
|
|
<P>
|
|
<BR> if (argc < 3) {
|
|
<BR> fprintf(stderr, "Usage: %s <locale> <string>\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Apply the specified locale */
|
|
<P>
|
|
<BR> if (setlocale(LC_ALL, argv[1]) == NULL) {
|
|
<BR> perror("setlocale");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Calculate the length required to hold argv[2] converted to
|
|
<BR> a wide character string */
|
|
<P>
|
|
<BR> mbslen = mbstowcs(NULL, argv[2], 0);
|
|
<BR> if (mbslen == (size_t) -1) {
|
|
<BR> perror("mbstowcs");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Describe the source string to the user */
|
|
<P>
|
|
<BR> printf("Length of source string (excluding terminator):\n");
|
|
<BR> printf(" %zu bytes\n", strlen(argv[2]));
|
|
<BR> printf(" %zu multibyte characters\n\n", mbslen);
|
|
<P>
|
|
<BR> /* Allocate wide character string of the desired size. Add 1
|
|
<BR> to allow for terminating null wide character (L'\0'). */
|
|
<P>
|
|
<BR> wcs = calloc(mbslen + 1, sizeof(wchar_t));
|
|
<BR> if (wcs == NULL) {
|
|
<BR> perror("calloc");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Convert the multibyte character string in argv[2] to a
|
|
<BR> wide character string */
|
|
<P>
|
|
<BR> if (mbstowcs(wcs, argv[2], mbslen + 1) == (size_t) -1) {
|
|
<BR> perror("mbstowcs");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> printf("Wide character string is: %ls (%zu characters)\n",
|
|
<BR> wcs, mbslen);
|
|
<P>
|
|
<BR> /* Now do some inspection of the classes of the characters in
|
|
<BR> the wide character string */
|
|
<P>
|
|
<BR> for (wp = wcs; *wp != 0; wp++) {
|
|
<BR> printf(" %lc ", (wint_t) *wp);
|
|
<P>
|
|
<BR> if (!iswalpha(*wp))
|
|
<BR> printf("!");
|
|
<BR> printf("alpha ");
|
|
<P>
|
|
<BR> if (iswalpha(*wp)) {
|
|
<BR> if (iswupper(*wp))
|
|
<BR> printf("upper ");
|
|
<P>
|
|
<BR> if (iswlower(*wp))
|
|
<BR> printf("lower ");
|
|
<BR> }
|
|
<P>
|
|
<BR> putchar('\n');
|
|
<BR> }
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+mblen">mblen</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+mbsrtowcs">mbsrtowcs</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+mbtowc">mbtowc</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+wcstombs">wcstombs</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+wctomb">wctomb</A></B>(3)
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="4"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="5"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="6"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="7"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="8"><A HREF="#lbAF">ATTRIBUTES</A><DD>
|
|
<DT id="9"><A HREF="#lbAG">CONFORMING TO</A><DD>
|
|
<DT id="10"><A HREF="#lbAH">NOTES</A><DD>
|
|
<DT id="11"><A HREF="#lbAI">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="12"><A HREF="#lbAJ">Program source</A><DD>
|
|
</DL>
|
|
<DT id="13"><A HREF="#lbAK">SEE ALSO</A><DD>
|
|
<DT id="14"><A HREF="#lbAL">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:48 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|