function

wcstombs

<cstdlib>
size_t wcstombs ( char * dest, const wchar_t * src, size_t max );
Convert wide-character string to multibyte string
Translates up to max characters of the C wide string src to their multibyte sequence equivalents and stores them in the buffer pointed by dest, stopping if a terminating null wide character is encountered (which is also translated and stored, but not counted in the length returned by the function).

The behavior of this function depends on the LC_CTYPE category of the selected C locale.

Parameters

dest
Pointer to an array of char elements at least max bytes long.
src
C wide string to be translated.
max
Maximum number of bytes to be written to dest.
size_t is an unsigned integral type.

Return Value

The number of bytes written to dest, not including the eventual ending null-character.
If an invalid multibyte character is encountered, a (size_t)-1 value is returned.
Notice that size_t is an unsigned integral type, and thus none of the values possibly returned is less than zero.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* wcstombs example */
#include <stdio.h>
#include <stdlib.h>

int main() {
  const wchar_t str[] = L"wcstombs example";
  char buffer[32];
  int ret;

  printf ("wchar_t string: %ls \n",str);

  ret = wcstombs ( buffer, str, sizeof(buffer) );
  if (ret==32) buffer[31]='\0';
  if (ret) printf ("multibyte string: %s \n",buffer);

  return 0;
}


Output:

wchar_t string: wcstombs example 
multibyte string:  wcstombs example 

See also