0

I want to convert a wide character string to a multi-byte character string.

The following program gives me an error:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define BUFFER_SIZE 100

int main( void )
{
    size_t   i;
    char      *pMBBuffer = (char *)malloc( BUFFER_SIZE );
    wchar_t*pWCBuffer = L"Hello, world.";

    printf( "Convert wide-character string:\n" );

    // Conversion
    wcstombs_s(&i, pMBBuffer, (size_t)BUFFER_SIZE,
               pWCBuffer, (size_t)BUFFER_SIZE );

    // Output
    printf("   Characters converted: %u\n", i);
    printf("    Multibyte character: %s\n\n",
     pMBBuffer );

    // Free multibyte character buffer
    if (pMBBuffer)
    {
    free(pMBBuffer);
    }
}

I compiled with a C++11 compiler. The error is:

error: ‘wcstombs_s’ was not declared in this scope; did you mean ‘wcstombs’?
   19 |     wcstombs_s(&i, pMBBuffer, (size_t)BUFFER_SIZE,
      |     ^~~~~~~~~~
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 4
    What compiler are you using? `wcstombs_s` is a Microsoft extension. – Drew Dormann Aug 23 '21 at 18:05
  • Now the real question is: Why do you want to trash your data? Converting (presumably) UTF-16 encoded text to any other encoding (that isn't UTF-8) is a lossy operation. – IInspectable Aug 23 '21 at 21:06
  • use WideCharToMultiByte function: https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte – Simon Mourier Aug 24 '21 at 05:17
  • @DrewDormann I use g++ -std=c++11 test_wchar_t.c – Younès RAOUI Aug 24 '21 at 09:25
  • @SimonMourier I work under Ubuntu, does this function exist ? – Younès RAOUI Aug 24 '21 at 09:26
  • 1
    You marked the question with [winapi] tag, WideCharToMultiByte is a Windows API, so it can work if you have a way to bind to a Windows API (if you have minGW for example https://stackoverflow.com/a/13101384/403671 where MessageBox is a Windows API). Of course the program would not be portable on other platform. – Simon Mourier Aug 24 '21 at 09:43
  • I changer the function wcstombs_s with wcstombs as following : – Younès RAOUI Aug 25 '21 at 12:40
  • it works fine by changing wcstombs_s to wcstombs which receives as parameters the pointers to the char array and the wchar_t array and the size of the char array. – Younès RAOUI Aug 25 '21 at 12:43

1 Answers1

0

I modified the funcion wcstombs_s to wcstombs as following : It works fine:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define BUFFER_SIZE 100

int main( void )
{
    size_t   i;
    char      *pMBBuffer = (char *)malloc( BUFFER_SIZE );
    wchar_t*pWCBuffer = L"Hello, world.";

    printf( "Convert wide-character string:\n" );

        wcstombs(pMBBuffer, pWCBuffer, (size_t)BUFFER_SIZE);



    // Output
    printf("    Multibyte character: %s\n\n",
     pMBBuffer );

    // Free multibyte character buffer
    if (pMBBuffer)
    {
    free(pMBBuffer);
    }
}
~  

                                                                                                                                                                                                    

~