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

C library function - mbtowc()

C library function - mbtowc()

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

mbtowc() is a function in the C standard library used to convert a multibyte character to a wide character.

mbtowc() Its declaration is located in header file, mainly used to support conversion between multibyte character sets (such as UTF-8) and wide character sets (such as Unicode).

mbtowc() Converts a multibyte character (mbstr) to a wide character (wc). The function determines how to interpret the multibyte character based on the current LC_CTYPE locale (environment setting). If the multibyte character is valid, it is converted to a wide character. If the character is invalid, it returns -1.

Declaration

Here is the declaration of the mbtowc() function.



#include 



int mbtowc(wchar_t *wc, const char *mbstr, size_t n);

Parameters:

Return Value

Example

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

Example

#include

#include

#include

#include

#include

int

main

(

)

{

   

// Set the program's locale information

   

setlocale

(

LC_CTYPE

,

""

)

;

   

// Multibyte string

   

const

char

str

=

"This is runoob.com"

;

   

// Wide-character array to store the converted result

   

wchar_t

mb

[

100

]

;

   

   

// Current character index and converted wide-character length

   

int

i

=

0

;

   

int

len

;

   

   

// Convert characters one by one to wide characters

   

while

(

str

)

{

        len

=

mbtowc

(

&

mb

[

i

]

,

str

,

MB_CUR_MAX

)

;

 

// Convert the current multibyte character

       

if

(

len

0

)

{

            str

+=

len

;

 

// Move to the start of the next character

            i

++;

 

// Store into the wide-character array

       

}

else

if

(

len

==

0

)

{

           

break

;

 

// String terminator encountered

       

}

else

{

           

// Error handling for invalid characters

           

printf

(

"invalid character\n"

)

;

           

break

;

       

}

   

}

   

// Output the converted wide-character string

   

wprintf

(

L

"%ls \n"

,

mb

)

;

   

   

return

0

;

}

Let us compile and run the above program, which will produce the following result, because it outputs in multibyte form, which is a kind of binary output.



这里是 runoob.com 

Example

#include

#include

#include

int

main

(

)

{

   

// Set the program's locale information

   

setlocale

(

LC_CTYPE

,

""

)

;

   

const

char

mbstr

=

"Hello" (Chinese)

;

   

wchar_t

wc

;

   

// Convert one character at a time

   

for

(

size_t

i

=

0

;

mbstr

[

i

]

!=

'\0'

;

i

++

)

{

       

int

len

=

mbtowc

(

&

wc

,

&

mbstr

[

i

]

,

1

)

;

       

if

(

len

0

)

{

           

wprintf

(

L

"converted wide character: %lc\n"

,

wc

)

;

            i

+=

len

1

;

 

// Skip the number of bytes of the current multibyte character

       

}

else

if

(

len

==

0

)

{

           

break

;

       

}

else

{

           

printf

(

"invalid character\n"

)

;

       

}

   

}

   

return

0

;

}

Explanation:

  1. Multibyte character sets: In a multibyte character set (such as UTF-8), one character may consist of multiple bytes, while in a wide character set (such as Unicode), one character is usually represented by a wchar_t type value (2 or 4 bytes on some platforms).

  2. Locale settings:mbtowc() behavior depends on the current program's locale setting, especially the LC_CTYPE setting, which determines how multibyte character sets are interpreted. Therefore, when using this function, it is usually necessary to set the appropriate locale via setlocale() to set the appropriate locale.

  3. Error handling: If the input multibyte string is malformed or cannot be converted to a wide character, the conversion fails.mbtowc() returns -1You should check the return value to ensure the conversion succeeded.

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