Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C library function - mbstowcs()

C library function - mbstowcs()

C Standard Library - <stdlib.h> C Standard Library -

Description

C Library Function mbstowcs Used to convert a multibyte string into a wide-character string.

mbstowcs The function is defined in header.

Declaration

Here is the declaration of the mbstowcs() function.



size_t mbstowcs(wchar_t *pwcs, const char *mbs, size_t n);

Parameters

Return Value

Example

The following example demonstrates the use of the mbstowcs() function.

Example 1

#include

<

stdio.h

#include

<

stdlib.h

#include

<

string.h

int

main

(

)

{

int

len

;

char

pmbnull

=

NULL

;

char

pmb

=

(

char

)

malloc

(

MB_CUR_MAX

)

;

wchar_t

pwc

=

L

"

Hi

"

;

wchar_t

pwcs

=

(

wchar_t

)

malloc

(

sizeof

(

wchar_t

)

)

;

printf

(

"

Convert to a multibyte string

\

n

"

)

;

len

=

wcstombs

(

pmb

,

pwc

,

MB_CUR_MAX

)

;

printf

(

"

Characters converted: %d

\

n

"

,

len

)

;

printf

(

"

Hexadecimal value of the first multibyte character: %#.4x

\

n

"

,

pmb

)

;

printf

(

"

Convert back to a wide-character string

\

n

"

)

;

len

=

mbstowcs

(

pwcs

,

pmb

,

MB_CUR_MAX

)

;

printf

(

"

Characters converted: %d

\

n

"

,

len

)

;

printf

(

"

Hexadecimal value of the first wide character: %#.4x

\

n

\

n

"

,

pwcs

)

;

return

(

0

)

;

}

Let's compile and run the above program, which will produce the following result:



转换为多字节字符串

被转换的字符 1

第一个多字节字符的十六进制值:0x19a60010

转换回宽字符字符串

被转换的字符 1

第一个宽字符的十六进制值:0x19a60030

Example 2

#include

#include

#include

#include

int

main

(

)

{

   

// Set the current locale to the one specified by the user environment variables

   

setlocale

(

LC_ALL

,

""

)

;

   

// Define the multibyte string

   

const

char

mbstr

=

"Hello, World!"

;

   

// Compute the required buffer size

   

size_t

wcs_len

=

mbstowcs

(

NULL

,

mbstr

,

0

)

1

;

   

// Allocate the wide-character buffer

   

wchar_t

wcs

=

(

wchar_t

)

malloc

(

wcs_len

sizeof

(

wchar_t

)

)

;

   

if

(

wcs

==

NULL

)

{

       

perror

(

"malloc"

)

;

       

return

1

;

   

}

   

// Perform the conversion

    mbstowcs

(

wcs

,

mbstr

,

wcs_len

)

;

   

// Print the wide-character string

   

wprintf

(

L

"%ls\n"

,

wcs

)

;

   

// Free memory

   

free

(

wcs

)

;

   

return

0

;

}

Let's compile and run the above program, which will produce the following result:

Hello, 世界!

Notes

  1. Locale: the function's behavior is affected by the current locale setting. Usually, before calling the function, it is mbstowcs necessary to call setlocale to set the appropriate locale.
  2. Error handling: If the input multibyte string is malformed or cannot be converted to a wide character, the conversion fails. (size_t)-1 and sets errnothe error type. The caller needs to check the return value and handle possible errors.
  3. memory allocation: the caller needs to ensure that pwcs has enough space to store the converted wide-character string. This can be done by calling mbstowcs, and replace pwcs to NULL once to compute the required buffer size.

C Standard Library - <stdlib.h> C Standard Library -