首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::mbtowc

Defined in header <cstdlib>

int mbtowc( wchar_t* pwc, const char* s, std::size_t n );

转换第一个字节指向的多字节字符。s写成一个宽的字,写到*pwc如果pwc不是空的。

如果s为空指针,重置全局转换状态并确定是否使用移位序列。

参数

s

-

pointer to the multibyte character

n

-

limit on the number of bytes in s that can be examined

pwc

-

pointer to the wide character for output

返回值

如果s不是空指针,返回包含在多字节字符或-1如果第一个字节指向s不要形成有效的多字节字符或​0​如果s指向空charcter。'\0'...

如果s为空指针,将其内部转换状态重置为表示初始移位状态并返回。​0​如果当前多字节编码不依赖于状态,则%28不使用Shift序列%29,如果当前多字节编码依赖于状态,则%28不使用Shift序列%29,则使用非零值。

注记

每次呼叫mbtowc更新内部全局转换状态%28a类型的静态对象std::mbstate_t,只知道此函数%29。如果多字节编码使用移位状态,则必须注意避免回溯或多次扫描。在任何情况下,多个线程都不应该调用mbtowc如果没有同步:std::mbrtowc可能会被使用。

二次

代码语言:javascript
复制
#include <iostream>
#include <clocale>
#include <cstring>
#include <cstdlib>
 
int print_mb(const char* ptr)
{
    std::mbtowc(NULL, 0, 0); // reset the conversion state
    const char* end = ptr + std::strlen(ptr);
    int ret;
    for (wchar_t wc; (ret = std::mbtowc(&wc, ptr, end-ptr)) > 0; ptr+=ret) {
        std::wcout << wc;
    }
    std::wcout << '\n';
    return ret;
}
 
int main()
{
    std::setlocale(LC_ALL, "en_US.utf8");
    // UTF-8 narrow multibyte encoding
    const char* str = u8"z\u00df\u6c34\U0001d10b"; // or u8"zß水?"
                      // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";
    print_mb(str);
}

二次

产出:

二次

代码语言:javascript
复制
zß水?

二次

另见

mbrtowc

converts the next multibyte character to wide character, given state (function)

mblen

returns the number of bytes in the next multibyte character (function)

do_in virtual

converts a string from externT to internT, such as when reading from file (virtual protected member function of std::codecvt)

c.MBTOC文件

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券