RAD Studio
ContentsIndex
PreviousUpNext
wcstombs

Header File 

stdlib.h  

Category 

Conversion Routines, Memory and String Manipulation Routines 

Prototype 

size_t wcstombs(char *s, const wchar_t *pwcs, size_t n); 

Description 

Converts a wchar_t array into a multibyte string. 

wcstombs converts the type wchar_t elements contained in pwcs into a multibyte character string s. The process terminates if either a null character or an invalid multibyte character is encountered. 

No more than n bytes are modified. If n number of bytes are processed before a null character is reached, the array s is not null terminated. 

The behavior of wcstombs is affected by the setting of LC_CTYPE category of the current locale. 

Return Value 

If an invalid multibyte character is encountered, wcstombs returns (size_t) -1. Otherwise, the function returns the number of bytes modified, not including the terminating code, if any. 

Example  

#include <stdio.h>
#include <stdlib.h>
void main(void)
{
  int x;
  char *pbuf = (char*)malloc( MB_CUR_MAX);
  wchar_t *pwcsEOL = L'\0';
  char *pwchi= L"Hi there!";
  
  printf (" Convert entire wchar string into a multibyte string:\n");
  x = wcstombs( pbuf, pwchi,MB_CUR_MAX);
  printf ("Character converted: %u\n", x);
  printf ("Multibyte string character: %1s\n\n",pbuf);
  printf (" Convert when target is NULL\n");
  x = wcstombs( pbuf, pwcsEOL, MB_CUR_MAX);
  printf ("Character converted: %u\n",x);
  printf ("Multibyte string: %1s\n\n",pbuf);
        
}

Portability

POSIX 
Win32 
ANSI C 
ANSI C++ 
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!